给定一个包含内容的文件input.txt
:
xyMnZnMs76XHgsWJ
size: YN3hxFqFeUxXUMrQ
USPqYEnbquwFhUW7
vhJJduHZu25qHzTw
size: JaTDhQYQtLRWBLjj
uE5HGDAZN6DHU73Q
QxVPfQQaKTgR9LXQ
grnsm8bwSz5fJbQS size:
7CcuZxCHSJYhcdqg
ZuWadAaLWFwDwj5Z
怎么用if (condition) {...} else {...}
?
我尝试使用:
awk -F ':' '
IF ($1 ~ /size/) {
print "match: "$0;
} ELSE {
print "non-match: "$0;
}' input.txt
但所有行都匹配(即使是不$1
包含 的行"size"
):
match: xyMnZnMs76XHgsWJ
match: size: YN3hxFqFeUxXUMrQ
match: USPqYEnbquwFhUW7
match: vhJJduHZu25qHzTw
match: size: JaTDhQYQtLRWBLjj
match: uE5HGDAZN6DHU73Q
match: QxVPfQQaKTgR9LXQ
match: grnsm8bwSz5fJbQS size:
match: 7CcuZxCHSJYhcdqg
match: ZuWadAaLWFwDwj5Z
我期望输出是:
non-match: xyMnZnMs76XHgsWJ
match: size: YN3hxFqFeUxXUMrQ
non-match: USPqYEnbquwFhUW7
non-match: vhJJduHZu25qHzTw
match: size: JaTDhQYQtLRWBLjj
non-match: uE5HGDAZN6DHU73Q
non-match: QxVPfQQaKTgR9LXQ
non-match: grnsm8bwSz5fJbQS size:
non-match: 7CcuZxCHSJYhcdqg
non-match: ZuWadAaLWFwDwj5Z