linux - General solution for bypassing file headers in shell commands -


i make extensive use of piping multiple linux shell commands, example:

grep blah file1 | sed 's/old/new/' | sort -k 1,1 > file3 

my files have header line, , have preserve throughout pipeline. so, example, want grep, sed , sort line 2 , on, while keeping 1st line unchanged.

i looking general solution given command(s) preserve header. write header file before pipe , cat after pipe ends. have started using zshell, wondering if might more streamlined solution.

perhaps this:

enter image description here

(arrows pipes in image)

but not sure how work in zshell or if possible. 1 problem need follow first pipe split command on both pipes.

any creative solutions?

vaughn , devnull have directed towards solution. both contain typos though , have remarks add , advise use instead:

{ head -n 1 file1; tail -n +2 file1 | grep blah | sed 's/old/new/' | sort -k 1,1; } >file3 

what take first line of file1 in 1 command (your header) , grep/sed/whatever magic in second command on rest of file (sans header, tail -n +2) , redirects combined output file3.

notes:

  • if shell supports { } preferred on ( ) construct in case not spawn subshell (sometimes desirable have subshell, though).
  • head -2 deprecated, should use -n parameter head -n 2
  • you can skip tail -n +2 file1 part if absolutely know grepping cannot found in header, cleaner way.

this should work in recent shells, btw (bash, ksh, zsh).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -