How can I delete every Xth line in a text file?

后端 未结 6 1512
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 05:07

Consider a text file with scientific data, e.g.:

5.787037037037037063e-02 2.048402977658663748e-01
1.157407407407407413e-01 4.021264347118673754e-01
1.736111         


        
6条回答
  •  不知归路
    2020-12-08 05:39

    You can use a awk and a shell script. Awk can be difficult but...

    This will delete specific lines you tell it to:

    nawk -f awkfile.awk [filename]
    
    awkfile.awk contents
    
    BEGIN {
    if (!lines) lines="3 4 7 8"
    n=split(lines, lA, FS)
    for(i=1;i<=n;i++)
     linesA[lA[i]]
    }
    !(FNR in linesA)
    

    Also I can't remember if VIM comes with the standard Ubuntu or not. If not get it.

    Then open the file with vim vim [filename]

    Then type

    :%!awk NR\%2 or :%!awk NR\%2 
    

    This will delete every other line. Just change the 2 to another integer for a different frequency.

提交回复
热议问题