Skip to main content
clarified input/output filenames; updated step 3 wording; added step "4" of capturing output
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
header=$(head -n 1 fileinput) (printf "%s\n" "$header"; grep -vFxe "$header" fileinput ) > newfileoutput 
  1. grab the header line from the input file into a variable
  2. print the header
  3. process the file with grep to omit lines that look likematch the header header
  4. capture the output from the above two steps into the output file
header=$(head -n 1 file) (printf "%s\n" "$header"; grep -vFxe "$header" file ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. process the file with grep to omit lines that look like the header
header=$(head -n 1 input) (printf "%s\n" "$header"; grep -vFxe "$header" input ) > output 
  1. grab the header line from the input file into a variable
  2. print the header
  3. process the file with grep to omit lines that match the header
  4. capture the output from the above two steps into the output file
you already " omit lines that look like the header" with 'grep' there's no need to ditch the 1st one via 'tail'
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262
header=$(head -n 1 file) (printf "%s\n" "$header"; tail -n +2 file|grepgrep -vFxe "$header" file ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. print everything exceptprocess the first line, andfile with grep to omit lines that look like the header
header=$(head -n 1 file) (printf "%s\n" "$header"; tail -n +2 file|grep -vFxe "$header" ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. print everything except the first line, and omit lines that look like the header
header=$(head -n 1 file) (printf "%s\n" "$header"; grep -vFxe "$header" file ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. process the file with grep to omit lines that look like the header
Adding -F for fixed string search, -x for whole line, -e to avoid problem with headers starting with "-". tail as a more obvious command to print the tail.
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k
header=$(head -n 1 file) (printf "%s\n" "$header"; sedtail 1d-n +2 file|grep -vvFxe "^$header$""$header" ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. print everything except the first line, and omit lines that look like the header
header=$(head -n 1 file) (printf "%s\n" "$header"; sed 1d file|grep -v "^$header$" ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. print everything except the first line, and omit lines that look like the header
header=$(head -n 1 file) (printf "%s\n" "$header"; tail -n +2 file|grep -vFxe "$header" ) > newfile 
  1. grab the header line into a variable
  2. print the header
  3. print everything except the first line, and omit lines that look like the header
added 2 characters in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
Loading
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
Loading