我有文字,包含一些行。 所以,我需要做几行GREP。 例如,我有重复的文字,我应该GREP得到线,有这个重复的关键词。
grep -o "test|test2" textfile
我的文字:
123|never for your|test 123421|never for your|test2 123412|never for your|test3 12341|never for your|test4 12311|never for your|test2 123312312|never for your|test 123321312|never for your|test2
我应该:
123|never for your|test 123421|never for your|test2 123312312|never for your|test 123321312|never for your|test2
它的工作,但它不工作,我想要的。 它在文本中search所有单词“test”和“test2”。 但是我想得到的文本块,就像一些模式,只有在“testing”来到“test2”之后。 你有什么想法吗?
简单的shell脚本使用sed。 制作第二种情况的行号列表,并与第一种情况的行号进行比较。 打印匹配的对。 使用第一个参数作为文件名。 可以很容易地扩展到第二个和第三个参数作为模式匹配。 可以保存为findnext.sh,然后运行:
$ sh findnext.sh testfile
应该很快,因为它只涉及文件两遍,并具有完全便携的优点。
#!/bin/sh # Line numbers matching test1 mt2=$(sed -ne '/test1/=' < $1 | tr '\n' '/') for l in $(sed -ne '/test/=' < $1); do nextline=$(expr $l + 1) [ "${mt2#*$nextline/}" != "$mt2" ] && sed -ne $l,${nextline}p <$1 done
您可以尝试grep -E或egrep。 请尝试像这样
#this will show lines that have test or test2 grep -E "test|test2" file
如果你想显示有test和test2的行,就像这个test | test2一样
# This will show lines that has test|test2 grep "test\|test2" file
awk可能是你的工具:
awk '/test$/, /test2$/' < block-text-lines.txt
一般forms是:
awk '/start-pattern/, /end-pattern/{command}'
但是由于命令块默认打印,所以只有开始和结束模式才能做到这一点。
请查阅man awk或“ Gnu Awk用户指南”了解更多细节。
grep -A 1 "test$" in.txt | grep -B 1 "test2$"
在grep手册
-A NUM在匹配行后打印NUM行结尾的上下文。
-B NUM在匹配行之前打印前导上下文的NUM行。
命令grep -Pzo ".*test$\n.*test2$" in.txt也可以,但在手册中是“这是高度实验性的,grep -P可能会警告未实现的function。