0

I have an input

text and numbers
 name of section

      72.01043451      0.013887  0.8416
      64.76001571      0.015442  0.8556

I want to print $1

/  name of section/ { in_f_format=1; next}
(in_f_format & FNR==2) {print($1)}

gives empty row and that $1 How to write print from second row?

This is syntax error

/  name of section/ { in_f_format=1; next}
(in_f_format & FNR==2) {print($1)}

two next doesn't work.

Thank you

Desired output:

  72.01043451
  64.76001571
1
  • 2
    You need to replace & with &&, but I don't know what you're exactly trying to achieve. FNR==2 is already specifying that only the 2nd line will be printed. If you're looking for the 2nd line after the pattern, that's a different story and you should not use FNR==2. Apr 27, 2019 at 6:32

3 Answers 3

1

In awk:

$ awk '/name of section/ { line = FNR + 2 } line != 0 && FNR >= line { print $1 }' file
      72.01043451
      64.76001571

Here we detect the pattern and set the variable line to the line that we'd like to print from (FNR + 2 means "this line plus two"). If we've reached the indicated line, we print.

1
  • This is more succinct and admittedly should be the accepted answer. Apr 27, 2019 at 7:56
1

If I understand your intention correctly,

/  name of section/ { c=FNR;}
(FNR-c>=2 && c>0) {print $1} 

If the pattern is matched, c is set to the current line number, then if FNR is equal to or greater than c+2, which is the 2nd line after the line that the pattern is matched, print the first field. Without initialization, c is zero, so we want to avoid that the 2nd line of the whole file be printed and therefore add an extra condition && c>0.

9
  • Thank you and this print only one row? I ment all row except the empty row Apr 27, 2019 at 6:48
  • 1
    Up to what? The end of the file? The next time the pattern is matched? Apr 27, 2019 at 6:52
  • 1
    See updated answer. Apr 27, 2019 at 6:57
  • 1
    It should work with text and number before. I tested. If you have a specific file that doesn't work, edit it into the question. Apr 27, 2019 at 7:20
  • 1
    Should be fine now. Apr 27, 2019 at 7:31
1

Tested and worked fine

awk '/name of section/{x=NR+3}(NR<=x){print}' l.txt | awk 'NR>2{print $1}' filename
72.01043451
64.76001571

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .