awk help

Hi,
I just started using linux, and I am trying to use awk to search for a string in a document. If it exists I would like to print the next 33 lines(there will be mulitple matches in these files). Ive tried variations of this form, but dont know how to print by line.

awk '{if ($4=1970) print ???? }' oldfile>newfile.dat

Thanks for any tips

Try:

awk 'c&&c--;/pattern/{c=33}' infile

If you want to include the matching line:

awk '/pattern/{c=33}c&&c--' infile

Add one to c if you want the matching line and the following 33 lines (total 34 lines).

Opps, I forgot to mention the first field for this line needs to say "start" as well can this be added in to that search pattern?
Let me try and clarify...
I am looking for these two things in the line and if they match I need all the lines after that until the next "start" This is much harder than I first thought.
start xxx xxx 1970

sorry about the initial confusion

It would help if you posted a sample data file using BB Codes and a desired output.

Assuming there's no matching 'stop' for 'start':

nawk '/^start.*1970/ {c++;print "--";next} c'

This might be a dumb question but I dont think I have the nawk command. Can this be run using just awk.
I am new to our system and it says nawk command not found

This is the format of the file. The output can be the same. I just need to extract the entire line with "start", and a value in the fourth column, and all of the following info until the next "start" for the entire file. Im sorry this might be confusing, I am very new to this.

start     -71.50000     40.37000    1981       1       7       8       1
      0.00000      7.43000   9999.00000
     25.00000      7.50000   9999.00000
     45.00000      7.51000   9999.00000
     60.00000      7.68000   9999.00000
     64.00000      8.72000   9999.00000
     66.00000     10.13000   9999.00000
     69.00000     10.82000   9999.00000
     81.00000     10.83000   9999.00000
start     -71.52700     40.15500    1981       1       7       7       2
      0.00000      9.51000   9999.00000
      4.00000      9.39000   9999.00000
     35.00000      9.39000   9999.00000
     64.00000      9.46000   9999.00000
     68.00000     10.69000   9999.00000
     70.00000     10.94000   9999.00000
     85.00000     10.98000   9999.00000

I hope this comes through alright

Try this:

awk '
/start/ {p=1;print $4;next}
/start/ && p {exit}
p' file

Regards

yes, use 'awk' instead.

Once again, please use BB Codes when posting code/data samples.

Also please provide a sample of the expected output based on your sample input.

Ok Ive tried to add some BB Codes, they are all new to me, I thought the bold strings could be used for searching in awk. I need the output to be identical to the input. I just only want data from 1970. There are an unknown number of lines after the line with start & 1970 I want them all if they are from 1970. Let me know if this helps clarify

[left]start -71.50000 40.37000 1970 1 7 8 1
0.00000 7.43000 9999.00000
25.00000 7.50000 9999.00000
45.00000 7.51000 9999.00000
60.00000 7.68000 9999.00000
64.00000 8.72000 9999.00000
66.00000 10.13000 9999.00000
69.00000 10.82000 9999.00000[/left]

awk '
/start.*1970/ {p=1;print $4;next}
/start/ && p {exit}
p' file

Regards

ok, this is still not clear, but.....

awk '
   /^start/ && $4=="1970" {p=1}
   /^start/ && $4 !="1970" {p=0}
   p' file

That seems to work! Sorry about all the confusion. Did I use BB codes correctly? I am mostly a windows user and never used them before.
Thanks

No, you need to use

```text
 code/data goes here
```

paradigm. Reread the posted link.

Ok,
Thanks for the heads up and the help!