I am using the command below to output a list of IP addresses on the one line with the statement "working IP" after each IP

cat eciremoteExec1.txt | sort | uniq -c | awk -F " " '{print $3}' | sed 's/$/ working IP/'  | sed -e '0,/ working IP/ s/ working IP//' | sed '$ d' | paste -s -d ' ' | sed 's/working IP$//'

The output looks like this: working IP working IP working 
IP working IP working IP 
working IP working IP 

I piped it into sed multiple times to get it to look as I wanted. Is there a better approach than using sed multiple times?

  • 4
    Advise what the file contains... post some data of file. – George Vasiliou Dec 17 '16 at 15:40
  • 4
    You can run multiple sed operations in a single sed call. – Grzegorz Oledzki Dec 17 '16 at 18:20

It's not only about combining multiple sed commands (Jasen expained that part in his answer). You can learn some more:

cat file | sort ...

cat is not needed, as you can give sort a filename, you don't need to cat before, but do

sort file | uniq -c | awk ...

uniq -c is removes duplicates and adds a count, but as the following awk uses only the third comlun, so the count is dropped anyhow. You can drop the -c and replace the whole uniq with option -u to sort:

sort -u file | awk -F " " '{print $2}' | sed ...

Here you are misusing awk for cut -d" " -f 2, but you are free to do so. But now you can continue to use awk or do that step in sed, too. No need to switch tools for a task that can be equally done in each tool:

sort -u file | sed 's/[^ ]* *\([^ ]*\).*/\1 working IP/' ...

Here it's starting to be weird without knowing the input file. But if you are appending working IP to each line, then remove it for all in the range until it's first occurence, it should be identical to appending on all lines but the first one:

sort -u file | sed 's/[^ ]* *//;s/ .*//;1!s/$/ working IP/'

Next, the paste can easily be included in sed by collecting the lines in the hold space and later replacing the first newlines with a whitespace, all others with working IP. At the same time the removal of the last line ( $ d) can be done, so the whole mile-long command line with 6 different commands and 8 pipes burns down to a simple sort, only one pipe and a short sed script:

sort -u file | sed 's/[^ ]* *//;s/ .*//;$!{H;1h;d;};x;s/\n/ /;s// working IP /g'

Identical result without spending a lifetime to read options in man pages and analyze what is happening at each stage.


it's probably possible to combine the first three sed calls like this

cat eciremoteExec1.txt | sort | uniq -c | awk -F " " '{print $3}' |
   sed 's/$/ working IP/;s/ working IP//; $ d' | paste -s -d ' ' |
   sed 's/working IP$//'

but it's hard to say without knowing what inputs are legal and what output is desired.

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.