I've a tabular file composed of the tabular data and a header that I must keep along during my workflow -and that complicates everything-. I was wondering if there's some way to omit the nth first lines in the file, use awk
(eg, for column filtering) and adding them again in the final output.
A possible solution (but a bit complex, I think) is to create a temporary file with the header, do awk 'NR>10 {command}'
and after add the the header again... something like this:
head -10 file > header_tmp.txt
awk 'NR>10 {$5 ~ /\//; print}' file | cat header_tmp.txt - > final_file
Maybe there's another more tricky solution ?
With awk, you can use NR keyword and do something like this :
awk 'NR<50{print}NR>=50{<code for lines >=50>}'
or
awk 'NR<50{print;next;}{<codes for lines >=50>}'
Example : I want to keep the 3 first lines, sum values for the others.
Input :
Hello World1
Hello World2
Hello World3
1;2;3;4
5;6;7;8
Code :
awk -F';' 'NR<4{print;next}{result=0;for(i=1;i<=NF;i++){result += $i}; print result}' test.txt
Result :
Hello World1
Hello World2
Hello World3
10
26
Easier with the example you provided :
awk -F\; 'NR<=10;(NR>10)&&($5 ~ /\//)' file
will print first ten lines and all the other lines where $5 satisfies /\//