Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Looking at your data, you might also be interested in our sister site: Bioinformatics. Commented Apr 3, 2023 at 13:11
  • 2
    Aside from the problem you asked about, for(x in a) can shuffle the order of your output lines, you should use for(x=1; x in a; x++) instead if you want to retain the input order. See gnu.org/software/gawk/manual/gawk.html#Scanning-an-Array. Commented Apr 3, 2023 at 13:34
  • 1
    @EdMorton Thanks! I was just wondering that too. Cheers Commented Apr 3, 2023 at 14:06
  • I assume your real code does need to look at each later line for something else in the same awk program? Otherwise you should just filter on the fly, without building an array at all, and exit after 5 records. A bit like cat -n | head -5 but with different formatting for the line numbers. Commented Apr 3, 2023 at 23:03
  • 1
    If you were going to do this with awk you should use '{print} NR==5{exit}' or {a[NR]=$0} NR==5{exit} END{ ... } instead of NR<6 {a[NR]=$0} END { ... } for better efficiency. Commented Apr 3, 2023 at 23:33