作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我必须模拟 egrep
加上其他命令的一些选项,主要是 awk
,但我不太了解如何在脚本中执行此操作。我知道 awk
可以识别正则表达式,但它可以替代 egrep
吗?
我真的不知道从哪里开始。
最佳答案
[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
或(如丹尼斯在评论中指出的那样)
[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
关于linux - 如何用其他命令模拟egrep?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8820152/
我是一名优秀的程序员,十分优秀!