3

I've been looking around and haven't found what I'm trying. I have to say I'm petty poor with grep, sed and awk though.

I have an alias:

alias upgradable='apt list --upgradable'

and it gets me what I need:

thunderbird/bionic-updates,bionic-security 1:68.4.1+build1-0ubuntu0.18.04.1 amd64 [upgradable from: 1:68.2.2+build1-0ubuntu0.18.04.1]
thunderbird-gnome-support/bionic-updates,bionic-security 1:68.4.1+build1-0ubuntu0.18.04.1 amd64 [upgradable from: 1:68.2.2+build1-0ubuntu0.18.04.1]

however I'd like to get only the first word, the header of it. Tried lots of stuff but all failed.

How do I have to proceed ?

  • First "word" would be thunderbird is that what you want? – jesse_b Jan 23 at 18:45
  • Yeah, that'd work ! However your question made me realise I'd like to have printed anything before the first / – Torof Jan 23 at 18:54
5

To print everything before the first / you can use cut:

alias upgradable='apt list --upgradable | cut -d'/' -f1

or awk:

alias upgradable="apt list --upgradable | awk -F'/' '{print \$1}'"
  • Doh thanks @steeldriver – jesse_b Jan 23 at 19:06
  • That's it. Seems so easy and obvious when you say it. Really gotta learn me some awk. Cheers ! – Torof Jan 23 at 19:07
  • 1
    @Torof: cut will be faster than awk however for something like this probably trivially so. – jesse_b Jan 23 at 19:08
0

Any regex will d.

alias upgradable='apt list --upgradable | perl -pe "s|\s.*||" '

your output: thunderbird/bionic-updates,bionic-security

or alias upgradable='apt list --upgradable | perl -pe "s|/.*||" '

your output: thunderbird

perl -pe will treat everything like one line, and '\n' as regular character. This way it will discard everything behind first white character (1'st example) or behind '/' (2'nd example), inlcudind those separators.

0

You mentioned grep:

apt list --upgradable | grep  -o "^.*\/" | cut -d'/' -f1  

I don't know how it compares for speed with awk or sed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.