使用bash或python从文件中提取行

时间:2016-05-12 06:25:30

标签: python bash awk sed grep

这是我的文件内容,它是pflogsumm

的输出
let ckeck = AssetCell()

输出似乎将信息字段分隔为&#34; title&#34;和#34;新线&#34;。例如EXC_BAD_INSTRUCTION 我尝试使用下面的sed表达式,但它匹配字符串Host/Domain Summary: Messages Received --------------------------------------- msg cnt bytes host/domain -------- ------- ----------- 415 5416k abc.com 13 19072 xyz.localdomain Senders by message count ------------------------ 415 alert@example.com 13 root@jelly.localdomain Recipients by message count --------------------------- 506 alert@apple.com <= Extract from here to ... 70 info@pafpro.org.us .. ... 19 gems@gmail.com 17 info@aol.com 13 hemdem@gmail.com <= Extract ends here Senders by message size ----------------------- 5416k alert@google.com ... ...

后返回所有行

Recipients by message count ...<contents of interest> ... NewLine

所需的输出:"Recipients by message count"

下的所有电子邮件

6 个答案:

答案 0 :(得分:4)

使用awk:

awk '/Recipients by message count/{p=1}!$0{p=0}p' input_file

将按消息计数块

打印收件人

故障:

/Recipients by message count/ {p=1} # When /pattern/ is matched set p = 1
!$0 {p=0}                           # When input line is empty set p = 0
p                                   # Print line if p is true, short for:
                                    # p { print $0 }

答案 1 :(得分:2)

$ sed -n '/Recipients by message count/,/^\s*$/ p' data | sed -n '1!{2!{$!p}}'
    506   alert@apple.com            <= Extracter from here to ...
     70   info@pafpro.org.us
     ..
     ...
     19   gems@gmail.com
     17   info@aol.com
     13   hemdem@gmail.com           <= Extract ends here

答案 2 :(得分:1)

这样的事情:

    findthis = "Recipients by message count"

    with open("tst.dat") as f:
      while True:
        line = f.readline()
        if not line: break

        if not findthis in line:
          continue
        line = f.readline()

        while True:
          line = f.readline()
          if not line: break
          line = line.rstrip()     ## get rid of whitespace
          if line == "":           ## empty line
            break
          print(line)

如果文件很大或您有通配符搜索,请使用正则表达式库。

答案 3 :(得分:1)

脚本下方:

sed -n '/Recipients/{n;n;:loop;/^$/!{p;n;b loop};q}' filename

将为您完成这项工作。

注意:如果感兴趣的模式位于最后,则需要一个尾随空行。

答案 4 :(得分:0)

一个awk命令,用于&#34;收件人&#34;之间的界限。和#34;发件人&#34;,如果该行以空格开头,则打印出来。

[name@server ~]$ awk '/^Recipients/,/^Senders/ { if ($0~/^ /) print }' input.txt
    506   alert@apple.com            <= Extracter from here to ...
     70   info@pafpro.org.us
     ..
     ...
     19   gems@gmail.com
     17   info@aol.com
     13   hemdem@gmail.com           <= Extract ends here

答案 5 :(得分:0)

另一个sed one liner:

 sed '/Recipients by message count/,/^$/!d;//{N;d};' file