1

I need some help with the following:

Write a single command that lists the features/capabilities of the CPU on your Linux machine, one per line, sorted alphabetically.

The single command may use pipes and re-direction, but must not simply combine commands (e.g., using ;).
An example of the first few lines of the contents is:

3dnewprefect
abm
aes
apic
...

This is what I have so far, but it’s not sorting it alphabetically:

lscpu | grep -i flags | sort -d
5
  • 1
    You need to convert the single line of flags into a list of one line per flag, then sort that list. Apr 4, 2021 at 8:46
  • Could you advise how best to do that.
    – Voiletta
    Apr 4, 2021 at 8:53
  • You could use sed, which would enable you to get rid of the grep as well. Replace strings of spaces with newlines: sed '/^Flags:/s/ */\n/g'. You then need to remove the Flags line from the output. Apr 4, 2021 at 8:55
  • 1
    I forgot you also need to remove all other lines from the output. You can do it in the same sed command by only printing the Flags line. This is accomplished with the -n option (don't print) and the p command: sed -n '/^Flags:/s/ */\n/gp'. Apr 4, 2021 at 8:59
  • Thanks for showing us the command you tried. It’s also helpful to show the output you got from that command. Apr 4, 2021 at 14:54

1 Answer 1

2

Using your example, I added adding a newline for each space and then got rid of the Flags: heading and removed any empty lines like so:

tr translates any space to a newline so that we can then ultimately sort it later because sort works on a line by line basis

egrep -v does an inverted match, which removes the selection (Flags or empty lines)

lscpu | grep -i flags | tr ' ' '\n' | egrep -v '^Flags:|^$' | sort -d

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .