[英]Extract Specific word from Text file between Specifc words in BASH
VAR="10.101.10.5"
我有以下格式的文本文件 abc.txt:
Name : www.ayz.com { Destination : 10.101.10.2 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
Name : www.abc.com { Destination : 10.101.10.5 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
要求是當我 grep 用於特定的 IP 時,例如10.101.10.5
。 它應該只搜索在單詞 Destination 之后和單詞 members 之前具有 IP 10.101.10.5
的行。
所以基本上我需要一個命令,僅當我 grep 為10.101.10.5
時才列出第二行,它不應該返回第一行,因為它在成員之后有 IP。
我嘗試使用sed
但它沒有幫助:
sed -n '/destination : $VAR /,/<members>/p' /abc.txt
我建議:
grep 'Destination .* 10\.101\.10\.5 .* members' abc.txt
更新:
grep "Destination .* ${VAR//./\\.} .* members" abc.txt
使用雙重配額和變量名。 您還可以使用一些正則表達式,例如grep "Destination\s*:\s*$VAR"以更靈活地處理 Destination 之后的額外空格
$ VAR="10.101.10.5"
$ grep "Destination : $VAR" file.txt
Name : www.abc.com { Destination : 10.101.10.5 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.