根据某些模式排除文件中的行

根据某些模式排除文件中的行

我的文件看起来像这样

[0.00137532,[0,13,19,16,18,15,19,16,11,15,12,12,13,14,0,11,17,18,14,17],[0,0,0,0,0,0,0,0,0,0,0,0.0189924,0.0871235,0.179813,0.307779,0$
SITE:   0        0.000853196055    0.0694597696 0000000001
[0.00111747753,[0,13,18,16,19,15,18,19,11,15,12,12,13,14,0,11,17,14,16,17],[0,0,0,0,0,0,0,0,0,0,0,0.018992411,0.0871235198,0.179812517$
[0.000200093646,[0,13,19,17,18,16,19,15,11,16,12,12,13,14,15,0,11,18,14,17],[0,0,0,0,0,0,0,0,0,0,0,0.018992411,0.0871235198,0.17981251$
[1.9658373e-05,[0,18,14,11,12,19,14,15,16,19,17,12,13,0,11,13,17,18,15,16],[0,0,0,0,0,0,0,0,0,0,0,0.106437198,0.163778333,0.758483056,$
[0.000282736441,[0,18,15,11,13,19,15,12,16,19,17,12,13,14,0,11,17,18,14,16],[0,0,0,0,0,0,0,0,0,0,0,0.106437198,0.129806881,0.163778333$
[0.00111187732,[0,13,19,16,18,15,19,17,11,15,12,12,13,14,0,11,17,18,14,16],[0,0,0,0,0,0,0,0,0,0,0,0.018992411,0.0871235198,0.179812517$
SITE:   1         0.00363901565     0.820587534 1000100111
[0.000647295926,[0,13,19,16,18,15,19,17,11,15,12,12,13,14,0,11,17,18,14,16],[0,0,0,0,0,0,0,0,0,0,0,0.018992411,0.0871235198,0.17981251$
[0.000272141,[0,11,19,16,18,15,19,17,13,15,14,12,0,14,11,13,17,18,12,16],[0,0,0,0,0,0,0,0,0,0,0,0.687401201,0.989300937,0.018992411,0.$
[1.82208814e-05,[0,11,16,13,15,19,16,14,17,19,18,12,0,14,15,11,13,18,12,17],[0,0,0,0,0,0,0,0,0,0,0,0.569817481,0.687401201,0.106437198$
[0.000160613913,[0,11,19,16,18,15,19,17,13,15,14,12,0,14,11,13,17,18,12,16],[0,0,0,0,0,0,0,0,0,0,0,0.687401201,1.05012976,0.018992411,$
SITE:   2         0.00509457547    0.0291019941 1000000000

如何获取一个新文件,其中排除以 SITE 开头的行(不必有空格)

答案1

有很多种方法:

  • grep

    grep -v '^SITE' input.txt >output.txt
    

    ^SITESITE在开头匹配,-v反转匹配。输入文件为input.txt,输出文件为output.txt

  • sed

    sed '/^SITE/ d' input.txt >output.txt
    

    d删除以 ( ) 开头的行 ( SITE) ^SITE

  • awk

    awk '$0 !~ /^SITE/' input.txt >output.txt
    

    如果行不是以SITE( $0 !~ /^SITE/)开头则打印

  • perl

    perl -ne '/^SITE/ || print' input.txt >output.txt
    

    相同的逻辑不同的系统,如果行不是以SITE

  • bash

    while IFS= read -r i; do [[ $i =~ ^SITE ]] || echo "$i"; done <input.txt >output.txt
    

    逐行读取输入文件并打印任何不以SITE( [[ $i =~ ^SITE ]] || echo "$i") 开头的行。

  • python

    #!/usr/bin/env python3
    with open('input.txt') as f:
        for line in f:
            if not line.startswith('SITE'):
                print(line, end='')
    

    如果行不是以SITE( not line.startswith('SITE')) 开头,则打印。

相关内容