[英]Extract string from a binary file on Windows and Linux using python/grep
[英]extract text from txt file in linux using grep when there is ambguity
我有一個名為l.txt的文件,其中包含以下數據,名字和姓氏。 當我傳遞名字時,輸出應為姓氏。 下面的查詢對我有用。
l.txt
name Tony Mcgill
name Jag John
name Jagmohan Singh
它適用於托尼名字。 但是當我搜索鋸齒時,會給出兩個結果,約翰和辛格。 但是我只需要約翰。 我該如何實現。
grep -e '^'"name jag" l.txt | awk '{print $3}'
我正在使用此命令。
您可以使用grep -w
匹配單詞:
grep -iw 'tony' file
name Tony Mcgill
或者在grep
使用單詞邊界:
grep -i '\<tony\>' file
要么:
grep -i '\btony\b' file
您也可以在awk中執行所有操作:
awk '$2~/\<Jag\>/ {print $3}' l.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.