我必须模拟egrep
以及其他命令的一些选项,主要是awk
,但我真的不明白如何在脚本中执行此操作。我知道awk
会识别正则表达式,但它可以替代egrep
吗?
我真的不知道从哪里开始。
答案 0 :(得分:3)
[jaypal:~/Temp] cat file
This is FirstLine
SecondLineEEE
AAAblablabla
ForthLineEEE
FifthLine
LastLine
egrep -n
[jaypal:~/Temp] egrep -n 'LastLine' file
6:LastLine
[jaypal:~/Temp] awk /LastLine/'{print NR":"$0}' file
6:LastLine
egrep -v
[jaypal:~/Temp] egrep -v 'LastLine' file
This is FirstLine
SecondLineEEE
AAAblablabla
ForthLineEEE
FifthLine
[jaypal:~/Temp] awk '/LastLine/{next}1' file
This is FirstLine
SecondLineEEE
AAAblablabla
ForthLineEEE
FifthLine
或(正如Dennis在评论中指出的那样)
[jaypal:~/Temp] awk '!/LastLine/' file
This is FirstLine
SecondLineEEE
AAAblablabla
ForthLineEEE
FifthLine
egrep -i
(仅限 gnu awk
)[jaypal:~/Temp] egrep -i 'lastline' file
LastLine
[jaypal:~/Temp] gawk -v IGNORECASE=1 '/lastline/' file
LastLine
egrep -w
(仅限 gnu awk
)[jaypal:~/Temp] egrep -w 'is' file
This is FirstLine
[jaypal:~/Temp] gawk '/\<is\>/' file
This is FirstLine
egrep -f
[jaypal:~/Temp] cat file
This is FirstLine
SecondLineEEE
AAAblablabla
ForthLineEEE
FifthLine
LastLine
[jaypal:~/Temp] cat patternfile
LastLine
is
[jaypal:~/Temp] egrep -f patternfile file
This is FirstLine
LastLine
[jaypal:~/Temp] awk 'NR==FNR{a[$0]++;next} {for (x in a) if ($0~x) print $0}' patternfile file
This is FirstLine
LastLine