print every nth line into a row using gawk

后端 未结 5 2085
面向向阳花
面向向阳花 2020-12-07 22:59

I have a very huge file in which I need to obtain every nth line and print it into a row.

My data:

1      937  4.320194
2      667  4.913314
3      9         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 23:40

    To print every second line, starting with the first:

    awk 'NR%2==1' file.txt
    

    To print every tenth line, starting with the tenth line:

    awk 'NR%10==0' file.txt
    

    To use this in a script, add the following to a file called script.awk:

    BEGIN {
        print "Processing file"
    }
    
    NR%10==0
    
    END {
        print "Finished processing"
    }
    

    Then execute:

    awk -f script.awk file.txt
    

提交回复
热议问题