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
  • 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. – Weijun Zhou Apr 27 at 6:32
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.

  • Thank you for you code – Lukáš Altman Apr 27 at 7:45
  • This is more succinct and admittedly should be the accepted answer. – Weijun Zhou Apr 27 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.

  • Thank you and this print only one row? I ment all row except the empty row – Lukáš Altman Apr 27 at 6:48
  • 1
    Up to what? The end of the file? The next time the pattern is matched? – Weijun Zhou Apr 27 at 6:52
  • 1
    See updated answer. – Weijun Zhou Apr 27 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. – Weijun Zhou Apr 27 at 7:20
  • 1
    Should be fine now. – Weijun Zhou Apr 27 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.