Output of dnf repolist

If I need to parse the output of dnf repolist, what rules should I use, to obtain the 3 fields? They are space-separated, and they can and do contain spaces too :frowning:

fedora                            Fedora 36 - x86_64                    enabled

This portal seems to strip extra spaces, but you probably realize that there are 29 spaces between the 1st and 2nd fields and 20 spaces between the 2nd and 3d.

I could probably hack and rely on the 3d column always being enabled or disabled literals but does it work the same in all locales?

Here’s a way to turn it into a CSV:

dnf repolist | perl -pe 's/^(.*\S)\s{3,}(\S.*+)$/"\1","\2"/g'

If you want to capture for all repos and include enabled/disabled:

dnf repolist --all | perl -pe 's/^(.*\S)\s{3,}(\S.*\S)\s+(\S.*)$/"\1","\2","\3"/g'

If you happen to need one set of regex to parse both, here you go:

dnf repolist --all | perl -pe 's/^(\S.*\S)\s{3,}(\S.*\S(?<!((dis|en)abled|status)$))(\s+(enabled|disabled|status))?$/"\1","\2","\6"/g'

Example Output

“repo id”,“repo name”,“status”
“euca2ools”,“Euca2ools 3.4 - x86_64”,“enabled”
“eucalyptus”,“Eucalyptus 5 - x86_64”,“enabled”
“fedora”,“Fedora 36 - x86_64”,“enabled”
“fedora-cisco-openh264”,“Fedora 36 openh264 (From Ci”,“enabled”
“fedora-cisco-openh264-debuginfo”,“Fedora 36 openh264 (From Ci”,“disabled”
“fedora-debuginfo”,“Fedora 36 - x86_64 - Debug”,“disabled”
“fedora-modular”,“Fedora Modular 36 - x86_64”,“enabled”
“fedora-modular-debuginfo”,“Fedora Modular 36 - x86_64”,“disabled”
“fedora-modular-source”,“Fedora Modular 36 - Source”,“disabled”
“fedora-source”,“Fedora 36 - Source”,“disabled”
“fedora-spotify”,“negativo17 - Spotify”,“enabled”
“fedora-spotify-debug”,“negativo17 - Spotify - Debu”,“disabled”
“fedora-spotify-source”,“negativo17 - Spotify - Sour”,“disabled”
“google-chrome”,“google-chrome”,“enabled”
“google-cloud-sdk”,“Google Cloud SDK”,“enabled”
“hashicorp”,“Hashicorp Stable - x86_64”,“enabled”
“hashicorp-test”,“Hashicorp Test - x86_64”,“disabled”
“phracek-PyCharm”,“Copr repo for PyCharm owned”,“disabled”
“rpmfusion-free”,“RPM Fusion for Fedora 36 -”,“enabled”
“rpmfusion-free-debuginfo”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-source”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-updates”,“RPM Fusion for Fedora 36 -”,“enabled”
“rpmfusion-free-updates-debuginfo”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-updates-source”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-updates-testing”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-updates-testing-debuginfo”,“RPM Fusion for Fedora 36 -”,“disabled”
“rpmfusion-free-updates-testing-source”,“RPM Fusion for Fedora 36 -”,“disabled”

Once again you are comparing apples to oranges.
dnf repolist only displays the enabled repos, it does not have a 3rd column.
dnf repolist --all shows all installed repos with the 3rd column showing enabled or disabled
There are also other options --enabled (which is the default with no options) and --disabled which only lists the disabled repos.

Knowing what you are asking about helps to narrow down what action you need to work with.

No you are not correct in stating there are 29 spaces between the 1st & 2nd fields. The second field is a column that begins at the same column always and the number of spaces is determined by the number of characters in the first column
Similarly there are not a fixed number of spaces between the 2nd & 3rd columns as that is determined by the number of characters in the second column and padded by spaces to the proper amount for the 3rd column to always begin in the proper column.

Yes this forum strips spaces in regular text, but that can be controlled by using the </> preformatted text tags available on the tool bar when you are entering your post so the entry remains formatted as you see it displayed on your screen.

Note that I edited your post and added the preformatted text tags so that line is seen as it appears on the screen.

Here you can clearly see the number of spaces varies according to the amount of text in the preceding column.


and here is the same thing in the preformatted text tags.

rpmfusion-nonfree-updates-testing                                           RPM Fusion for Fedora 36 - Nonfree - Test Updates                   disabled
rpmfusion-nonfree-updates-testing-debuginfo                                 RPM Fusion for Fedora 36 - Nonfree - Test Updates Debug             disabled
rpmfusion-nonfree-updates-testing-source                                    RPM Fusion for Fedora 36 - Nonfree - Test Updates Source            disabled
updates                                                                     Fedora 36 - x86_64 - Updates                                        enabled

and without the tags
rpmfusion-nonfree-updates-testing RPM Fusion for Fedora 36 - Nonfree - Test Updates disabled rpmfusion-nonfree-updates-testing-debuginfo RPM Fusion for Fedora 36 - Nonfree - Test Updates Debug disabled rpmfusion-nonfree-updates-testing-source RPM Fusion for Fedora 36 - Nonfree - Test Updates Source disabled updates Fedora 36 - x86_64 - Updates enabled

Once again you insult me with your condescending attitude. Drop it, if you come here to answer questions.

If I missed a parameter in my question, it does not mean that I compare apples and oranges.

And yes, I am correct that there are 29 spaces between fedora and Fedora 36. I had counted each of them. That other entries may have a different number of them is inconsequential to this question. The specific figure was only used to augment the formatting by this portal. So, once again, drop the snide attitude. You come across as a very rude and angry peer. This is against this resources code of conduct. I hope moderators take notice.

Jeff is right to point out that there is a difference in output between the two commands. The example I gave here covers both cases. Does it solve your issue?

Unfortunately, it does not because I cannot rely on the assumption that the user has perl installed. This is rather a question of formatting rules than about a programmatic solution to them. Possible answers can include formatting definition rather than code to implement them.

I believe Perl is installed by Default in Fedora 36 Workstation. What is the target environment if not that?

It can be anything. I have no control of that.

Since this is a Fedora support channel, the assumption is that you’re asking about a currently supported version of Fedora. I can’t guarantee that the same example I gave you will work against CentOS 7 or Mandriva and this wouldn’t be the appropriate place to ask for that. As current versions of Fedora offer perl, that’s an option. You could try porting the regex to sed or python otherwise.

1 Like

@habono Parsing command output meant for human viewers is always kind of frail. Have you looked at using the DNF API to get the information you want?

I definitely can’t guarantee that the spacing will be the same on all locales — or that we’ll keep the repo names the same. If you really, really want to parse the output, you might want to consider a tool like awk or cut (which are very likely to be installed). These let you work with “fields”, which can be white-space separated.

A simple solution using GNU coreutils only:
dnf repolist | sed 's/ */ /g' | cut -d ' ' -f 2- | awk 'NR>1'

sed will replace the multiple contiguous spaces with one space, allowing spaces to be easily used as delimiter, cut is truncating the first field and awk is skipping the first line.

Sample output:

Fedora 36 - x86_64
Fedora 36 openh264 (From Cisco) - x86_64
Fedora Modular 36 - x86_64
download.vscodium.com
RPM Fusion for Fedora 36 - Free
RPM Fusion for Fedora 36 - Free - Updates
Fedora 36 - x86_64 - Updates
Fedora Modular 36 - x86_64 - Updates
1 Like

Is it available on remote machines running arbitrary other OSs?

I can certainly ask questions about the current version but the users of my scripts have no knowledge of any such, now or in the future. I have no way of knowing what they are running and in what configuration.

I’ve found an easy solution: split on the index of the column headers.

Since you found a solution, it would be nice to post it for future users with a similar problem. And update your title to something like “Get dnf repo names from dnf repolist command”

1 Like

There is a presumption, based on the fact that you are asking about output from dnf repolist, that the users of your solution are using fedora and that they will be using the same commands (or at least your scripts will). Thus your scripts would be based on the current dnf outputs and supposedly would have no way to self-modify should the data being processed change format in the future. Someone would need to be able to modify the scripts should the input data change.

To me this seems you do know what your users are running (Fedora) and although you may have no way to know what else they are running, or even what version, your script needs seem to be specifically aimed at Fedora and dnf. Thus claiming ignorance seems short sighted for future support of your users.

Although I have been using fedora for many years (from the very beginning) I have no way to know what changes may have been made in dnf over time and would personally have to do research from the beginning to see the details of changes made. I would anticipate that further changes are likely so whatever I might script/program/build today may not be correct for tomorrows needs. Hopefully things are stable, but who really knows. The need for maintenance should be anticipated.

The comment was made about DNF API, and that seems a reasonable point to base any scripts upon, rather than the text output of some individual dnf command

Mostly based on python, it should be available on any system that uses dnf. Dnf cannot be used where it is not installed/supported.

If those machines have DNF on them, yes. If they don’t, you also don’t have dnf repolist, though, do you?

The DNF API may change, but API changes at least should be versioned and documented. The output isn’t meant for computer consumption, though, so it is very conceivable that some future version will format this differently — or for that matter that past versions already have.

Sure, if that works for you, cool. But there’s no guarantee that those column headers will stay consistent.

I was referring to remote machines that interrogate Fedora hosts with DNF. My scripts access hosts remotely. So I am wondering if they have access to DNF API remotely, from an arbitrary OS that is not Fedora.

You can run ls -l /etc/yum.repos.d on the remote machines. This will list the repositories files on the target system.