awk start printing based on a condition
Using ed
:
$ printf '%s\n' '/^test/+2,$p' | ed -s file
1,2
3,3
In the ed
editor, the command /^test/+2,$p
would print (p
) the lines from two lines beyond the line matching ^test
, to the end ($
).
Using awk
:
$ awk '/^test/ { flag = 1; count = 1 }; (flag == 1 && count <= 0); { count-- }' file
1,2
3,3
Here, a line will be printed if flag
is 1 and if count
is less than or equal to zero. The flag is set to 1 when the pattern ^test
is matched in the input data, and count
is then also set to the number of lines to skip until the output should start (not counting the current line). The count
is decreased for all lines.
A slightly different approach with awk
:
$ awk '/^test/ { getline; while (getline > 0) print }' file
1,2
3,3
Here, we match our pattern and then immediately read and discard the next line of input. Then we use a while loop to read the rest of the file, printing each line read.
The exact same approach, but with sed
:
$ sed -n -e '/^test/ { n' -e ':again' -e 'n; p; b again' -e '}' file
1,2
3,3
Match the pattern, then read and discard the next line (n
), then get into a loop reading and printing each line (n; p;
). The loop is made up of the label again
and the branching/jumping to this label (b again
).
If you know your data starts 2 lines after test
, and there are no more lines with test
on them, you can get away with something like this:
awk '/^test$/ { f=1 } f && f++ > 2' filename
Also, to send this data to Gnuplot, you might consider doing it through a pipe like this:
(
echo "set datafile separator ','"
echo "plot '-' using 1:2 with lines"
awk '/^test$/ { f=1 } f && f++ > 2' filename
echo "e"
) | gnuplot -persist