0

我的文件是 a.txt:

this is for testing
so test
againa and again
zzz and ssss
this is for testing
so test
againa and again

在这里,我试图在 zzz 和测试之间提取测试:

 cat a.txt | sed -n '/zzz/,/test/p'

输出:

 zzz and ssss
 this is for testing
 so test

问题是:

cat a.txt | sed -n '/zzz/,/jjj/p'

当我试图保留文件中不存在的某些单词(jjj)时,它会为我提供从zzz文件到末尾的数据。理想情况下,它不应该返回任何东西。

4

3 回答 3

1

sed 并不像您希望的那样聪明。您可以使用 awk: 在看到第一个正则表达式后,存储这些行。当您点击第二个正则表达式时,打印出您捕获的所有行

awk -v regex1="zzz" -v regex2="jjj" '
    $0 ~ regex1 {start=1} 
    start {lines = lines $0 ORS} 
    start && $0 ~ regex2 {printf "%s", lines; exit}
'
于 2013-10-22T20:24:08.137 回答
0

另一个解决方案,只是为了好玩:

[ ~]$ awk 'BEGIN{b=e=0; s=es=""} 
      ($0 ~ "^zzz.*"){b=1} 
      ($0 ~ ".*test$"){e=1; b=0; es=s; s=""; if(es!=""){es=es"\n"$0}else{es=$0}} 
      (b==1){if(s!=""){s=s"\n"$0}else{s=$0}} END {print es}' file

具有相同输入文件的输出:

zzz and ssss
this is for testing
so test

如果您将 ".*test$" 更改为与输入文件中的单词不对应的另一个模式,则此命令将不会产生输出:

[ ~]$ awk 'BEGIN{b=e=0; s=es=""} 
      ($0 ~ "^zzz.*"){b=1} 
      ($0 ~ ".*jjj$"){e=1; b=0; es=s; s=""; if(es!=""){es=es"\n"$0}else{es=$0}} 
      (b==1){if(s!=""){s=s"\n"$0}else{s=$0}} END {print es}' file
[ ~]$

当然,您可以使用“-v”选项使正则表达式易于配置。

否则,带有 grep 的 anubhava 的提议不适用于我的笔记本电脑:

[neumann@MacBookPro ~]$ cat file
this is for testing
so test
againa and again
zzz and ssss
this is for testing
so test
againa and again
[neumann@MacBookPro ~]$ grep -oP 'zzz[\s\S]*test' file
[neumann@MacBookPro ~]$ grep --version
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
[neumann@MacBookPro ~]$ 

这就是为什么当我有一个包含多行的模式时,我使用 awk 来执行此操作。

于 2013-10-22T22:24:46.827 回答
0

grep -oP这里将是更好的选择:

$ grep -oP 'zzz[\s\S]*test' a.txt 
zzz and ssss
this is for testing
so test

grep -oP 'zzz[\s\S]*jjj' a.txt
于 2013-10-22T19:55:50.227 回答