【问题标题】:extract the lines from file with bash or python使用 bash 或 python 从文件中提取行
【发布时间】:2016-09-07 19:26:40
【问题描述】:

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

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 ...&lt;contents of interest&gt; ... NewLine 我尝试使用下面的 sed 表达式,但它在匹配字符串 "Recipients by message count"

后返回所有行

sed -nr '/.*Recipients by message count/,/\n/ p'

所需输出:"Recipients by message count" 下的所有电子邮件

【问题讨论】:

  • 不清楚,想要的输出是什么!!!请准确。
  • 为了清楚起见更新了帖子

标签: python bash awk sed grep


【解决方案1】:

使用 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 }

【讨论】:

  • 你的解决方案就像一个魅力,你能告诉我们{p=1}!$0{p=0}p'在这里做什么。谢谢
  • @satch_boogie 更新了故障,请让我知道你明白了。
【解决方案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

【讨论】:

  • 这个sed -n '1!{2!{$!p}}'可以缩短为sed -n '3,$p'
【解决方案3】:

类似这样的:

    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)

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

【讨论】:

    【解决方案4】:

    一个 awk 命令,用于“收件人”和“发件人”之间的行,如果该行以空格开头,则打印它。

    [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】:

      下面的脚本:

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

      将为您完成这项工作。

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

      【讨论】:

        【解决方案6】:

        另一个 sed 一个班轮:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-23
          相关资源
          最近更新 更多