Search code examples
linuxcontrollerpipeline

I can't quite get the results to be fully unique - Linux pipelining


I'm almost there... I'm supposed to end up with is 340 unique services. So far, I can only get it down to 341.

These are my tasks:

  1. Extract all the service names from the file.
  2. Sort the names alphabetically removing any duplicates.
  3. Remove any blank lines or lines that do not contain letters of the alphabet.
  4. Capture the final output to a file named 'uniqueservices.txt'.
  5. Count the lines in the file using a conditional command that is only executed if the previous combined commands are successful.

This is the command I used:

cat /etc/services | grep -Ev '^#|^$' | cut -f1 | sort -u > uniqueservices.txt && wc -l uniqueservices.txt

Here's what I should get:

This is what I should get

This is what I actually get:

What I actually get

I'm guessing there are (as always) better ways of doing this but... hey, I'm new to this. So close though!

Thanks in advance.

S


Solution

  • The cut -f1 part of your command determines what "column 1" means by scanning the lines for TAB separators. The unmatched line you're seeing likely just uses a space character instead. I suppose the easiest thing to do would be to just run cut again but looking for spaces instead of TABs.

    cat /etc/services | grep -Ev '^#|^$' | cut -f1 | cut -d' ' -f1 | sort -u > uniqueservices.txt && wc -l uniqueservices.txt