awk pattern matching in place of sed

I have written a script to parse data from some files on a Solaris 10 system and send the output to a CSV formatted file. The code snipped i am using to pull the data is as follows....

src_line=$(sed -n "/^search_pattern$/{=;}" $file)
for i in $src_line
do
start_line1=$(( i + 9 ))
nawk -v startline1="$start_line1" '
BEGIN{i=0; printf "%s, %s, %s, %s, %s\n", "Header 1", "Header 2", "Header 3", "Header 4", "Header 5"}
NR>=startline1 {if ($2 ~ /^[0-9]/)
{if ($4 ~ /^[0-9]/)
{fld4=$4}
else{fld4=""}
if (i == 0)
{i=1; printf "%s, %s, %s, %s, %s\n", $2, fld4, $6, $8, $10}
else{i=0; printf "%s, %s, %s, %s, %s\n", $2, fld4, $6, $8, $10}}
else{exit}}' $file > results.csv
done

I use SED to find the pattern in the file and report back with a line number.
It is possible that there can be more than one match in a file and all matches must be processed. After the match is found i skip n line, 9 in this case. If the vaule of filed 2 is numeric then Fileds 2,4,6....n are printed, If not then i know i have reached the end of the table which i am pulling data from.
I am trying to find a soulution to use AWK entirely for the process above but so far i have had no luck.
I can get AWK to find the pattern match, but i am unable to get it to skip the 9 lines.

Below is a sample from of data from a file i am processing

Table Header
++++++++++++++++++++++++++++++++++++++++++++++
+ Data Desp +
++++++++++++++++++++++++++++++++++++++++++++++
+ + + AV +
+ + +++++++++++++++++++++++++++++++++
+ + + A + B + C + D +
+ Col + Pos + + + + +
++++++++++++++++++++++++++++++++++++++++++++++
+ 1 + 1 + 11 + 3 + 0.1 + 0.2 +
+ 2 + 2 + 13 + 3 + 0.1 + 0.3 +
+ 3 + 1 + 9 + 3 + 0.1 + 0.4 +
+ 4 + 2 + 5 + 3 + 0.1 + 0.5 +
+ 5 + 1 + 21 + 3 + 0.1 + 0.6 +
+ 6 + 2 + 8 + 3 + 0.1 + 0.1 +
+ 7 + 1 + 7 + 3 + 0.1 + 0.4 +
+ 8 + 2 + 18 + 3 + 0.1 + 0.6 +
+ 9 + 1 + 45 + 3 + 0.1 + 0.7 +
+ 10 + 2 + 6 + 3 + 0.1 + 0.0 +
++++++++++++++++++++++++++++++++++++++++++++++

PS daa should look like this when opens in a plain txt editor.

Regards
Denis.

In awk, try

... 
/search pattern/ {L=NR+9}
NR>=L            {...}
1 Like

I tried the above piece of code and it does give me the correct line to start on, but if i try to run with the code below i get no output.

nawk '/search pattern/ {L=NR+10} 
NR>=L {if ($2 ~ /^[0-9]/){print $2} else{exit}}' filename

I though it might be possile to use a for loop to run through any value stored in the varible L but i get an error which states

nawk: can't assign to L; it's an array name.

Denis.

What's your search pattern? Without knowing that it's quite difficult to interpret the awk script's behaviour.

Assuming the pattern you search for be ++++... , this will print the desired fields from your input sample:

awk     'NR<L           {next}
         /^\+\+\+\+/    {if (L) exit; else L=NR+8}
         $2 ~ /^[0-9]/  {print $2, $4, $6}
        ' file
1 1 11
2 2 13
3 1 9
4 2 5
5 1 21
6 2 8
7 1 7
8 2 18
9 1 45
10 2 6

It will NOT scan for further occurrences of the pattern, but this could easily be implemented

Sorry for the delayed response.
The search pattern will always be the section "Table Header".
An example of header would be "Output Offset 1". The target file i am scanning is made up of 100's of instances of such tables all with different headers.
The number of occurences of rows listed in the table will range from 1 to N.
I will never know beforehand the number of rows, so this is why i have always checked to see if field 2 is numeric or not. If its not numeric i know i have reached the end of the table.

Regards
Denis.