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

后端 未结 6 1501
佛祖请我去吃肉
佛祖请我去吃肉 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:26

    This might work for you (GNU sed):

    seq 10 | sed '0~2d' # delete every 2nd line
    1
    3
    5
    7
    9
    seq 100 | sed '0~10!d' # delete 9 out of 10 lines
    10
    20
    30
    40
    50
    60
    70
    80
    90
    100
    
    0 讨论(0)
  • 2020-12-08 05:29

    This is easy to accomplish with awk.

    Remove every other line:

    awk 'NR % 2 == 0' file > newfile
    

    Remove every 10th line:

    awk 'NR % 10 != 0' file > newfile
    

    The NR variable in awk is the line number. Anything outside of { } in awk is a conditional, and the default action is to print.

    0 讨论(0)
  • 2020-12-08 05:29

    You could possibly do it with sed, e.g.

    sed -n -e 'p;N;d;' file # print every other line, starting with line 1
    

    If you have GNU sed it's pretty easy

    sed -n -e '0~10p' file # print every 10th line
    sed -n -e '1~2p' file # print every other line starting with line 1
    sed -n -e '0~2p' file # print every other line starting with line 2
    
    0 讨论(0)
  • 2020-12-08 05:30

    Try something like:

    awk 'NR%3==0{print $0}' file
    

    This will print one line in three. Or:

    awk 'NR%10<9{print $0}' file 
    

    will print 9 lines out of ten.

    0 讨论(0)
  • 2020-12-08 05:36

    How about perl?

    perl -n -e '$.%10==0&&print'       # print every 10th line
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题