嘿伙计们快速提问
我的文件看起来像:
L 0 256 * * * * *
H 0 307 100.0 + 0 0
S 30 351 * * * * *
D 8 27 * * * * 99.3
C 11 1 * * * * *
Run Code Online (Sandbox Code Playgroud)
对于我的脚本,我想首先使用$ 1为某些行创建awk打印$ 0
如
awk '{if ($1!="C") {print $0} else if ($1!="D") {print $0}}'
Run Code Online (Sandbox Code Playgroud)
但是,必须有一种方法将"C"和"D"组合成一个IF语句......对吗?
例如,如果我想搜索== L,H,S ie ... NOT C或D我怎么会这样做?
只是好奇:D
谢谢
乔纳森
P.P*_*.P. 10
您现在的情况不正确,$1!="C"并且$1!="D"不能同时出现错误.因此,它将始终打印整个文件.
这将按照您的描述进行:
awk '{if ($1!="C" && $1!="D") {print $0}}' file
Run Code Online (Sandbox Code Playgroud)
使用awk,您可以使用语法为特定模式提供规则
awk 'pattern {action}' file
Run Code Online (Sandbox Code Playgroud)
有关模式定义,请参阅awk手册页.在您的情况下,您可以使用正则表达式作为具有语法的模式
awk'/regular expression/ {action}' file
Run Code Online (Sandbox Code Playgroud)
并且可以满足您的需要的基本正则表达式
awk '/^[^CD]/ {print $0}' file
Run Code Online (Sandbox Code Playgroud)
你可以实际缩短为
awk '/^[^CD]/' file
Run Code Online (Sandbox Code Playgroud)
因为{print $0}是评论中建议的默认操作.