Home > other >  All but the last part of a pipeline is ignored, using sed file | sed file | head file
All but the last part of a pipeline is ignored, using sed file | sed file | head file

Time:10-17

I am trying to use pipe command in UNIx to substitute some words (sed) and then use the head command to show me the first 9 lines of the file. However, the head command erase the sed command and just do it by itself. Here is what I am trying to do:

$ sed 's/[n]d/nd STREET/g' street | sed 's/[r]d/rd STREET/g' street | head -n 9 street
01 m motzart        amadeous        25 2nd 94233
02 m guthrie        woody           23 2nd 94223
03 f simone         nina            27 2nd 94112
04 m lennon         john            29 2nd 94221
05 f harris         emmylou         20 2nd 94222
06 m marley         bob             22 2nd 94112
07 f marley         rita            26 2nd 94212
08 f warwick        dione           26 2nd 94222
09 m prine          john            35 3rd 94321

CodePudding user response:

sed and head only read from stdin when they aren't given a filename to read from instead. Therefore, when you give head the name street, it ignores its standard input (which is where the output from sed is).

Provide the filename only once, at the front of the pipeline.

$ <street sed -e 's/[n]d/nd STREET/g' -e 's/[r]d/rd STREET/g' | head -n 9

By the way, you could also write this to use only one sed operation to handle both nd and rd:

$ <streed sed -e 's/\([rn]d\)/&1 STREET/g' | head -n 9
  • Related