[英]AWK - Print complete input string after comparison
我有一個文件a.text:
hello world
my world
hello universe
如果第二個單詞是“world”,我想打印完整的字符串:
[root@sc-rdops-vm18-dhcp-57-128:/var/log] cat a | awk -F " " '{if($2=="world") print $1}'
hello
my
但我想要的輸出是:
[root@sc-rdops-vm18-dhcp-57-128:/var/log] cat a | awk -F " " '{if($2=="world") print <Something here>}'
hello world
my world
有關如何做到這一點的任何指示?
提前致謝。
awk '{if ($2=="world") {print}}' file
輸出:
hello world my world
首先,由於您正在編寫單個if語句,因此可以使用awk 'filter{commands;}'
模式,就像這樣
awk -F " " '$2=="world" { print <Something here> }'
要打印整行,您可以使用print $0
awk -F " " '$2=="world"{print $0}' file
可以寫成
awk -F " " '$2=="world"{print}' file
但{print}
是默認操作,因此在過濾器之后可以省略它,如下所示:
awk -F " " '$2=="world"' file
或者甚至沒有-F選項,因為空間是默認的FS值
awk '$2=="world"' file
如果你想/必須使用awk
來解決你的問題:
awk '$0~/world/' file.txt
如果一行(即$0
)與字符串“world”匹配(即~/world/
),則打印整行
如果您只想檢查world
的第二列:
awk '$2 == "world"' file.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.