1

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:

147.123.41.112 working IP 129.138.70.113 working IP 173.230.45.54 working 
IP 133.218.250.56 working IP 136.203.152.128 working IP 117.22.29.254 
working IP 117.3.11.111 working IP 127.59.131.236 

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
3

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.

0

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.