I have test data in a file text.txt
a
b
test
test2
1,2
3,3
I want to output the file starting from the line number where test is + 2. I need this to be a oneliner usable in gnuplot
, i have comeup with the following:
awk -v linestart=$(awk '$0~"test" {a=NR}END{print a+2}' $filename) 'BEGIN{FS=",";OFS="\t";lines}NR>=linestart{print $1, $2}' $filename
but i need somehow to supply the file contents to two awk
's which i do not know how to do. So i came up with solution with the $filename
but this has the problem, how to get the $filename
in.
I was thinking along the lines:
echo "test.txt" | read filename | awk -v linestart=$(awk '$0~"test" {a=NR}END{print a+2}' $filename) 'BEGIN{FS=",";OFS="\t";lines}NR>=linestart{print $1, $2}' $filename
but that does not work.
How else can i make the above work? The obvious problem is that i need to know the number of the line where i want to start printing before i run awk
. i was also thinking something along this:
awk 'BEGIN{FS=",";OFS="\t";lines=100000}{if ($0~"test"){lines=NR+2}; if(NR>=lines){print $1, $2}}'
But i did not even try it since, it is very ugly and not general, i have to make the variable lines
always sufficiently big. So is there an elegant solution that would work with a normal text file pipe or in the other case with some way of pushing the file name inside?
awk
method? Specifically, where the printing done? I found that it seems to be following(flag == 1 && count <= 0)
but i don't understand the syntax here. – atapaka Feb 04 '20 at 23:42awk
,print $0
is the default action – every record that passes the testflag == 1 && count <= 0
gets printed – Feb 05 '20 at 02:15