1
給出一個IP地址的IP地址192.168.10.21.somebody.com.br 我需要提取只是192.168.10.21我試過低於切入,它給「切:無效字節或字段列表「。如何剝離後的字符串
cut -d'。' -f-4
給出一個IP地址的IP地址192.168.10.21.somebody.com.br 我需要提取只是192.168.10.21我試過低於切入,它給「切:無效字節或字段列表「。如何剝離後的字符串
cut -d'。' -f-4
$ echo "192.168.10.21.somebody.com.br" | cut -d'.' -f -4
192.168.10.21
適合我!
所有這三個以下的假設你有存儲在參數
dom_name=192.168.10.21.somebody.com.br
比使用cut
,假設第一標籤刪除不以數字開頭更有效的域名:
echo "${dom_name%%.[[:alpha:]]*}"
如果第一個標籤可能是以數字開頭,這些仍然比cut
更有效,但是更醜陋,輸入時間也更長:
# Match one more dot than necessary to shorten the regular expression;
# then trim that dot when echoing
[[ $dn =~ (([0-9]+\.){4}) ]]
echo "${BASH_REMATCH[1]%.}"
或
# Split the string into an array, then output the
# first four fields rejoined by dots.
IFS=. read -a labels <<< "$dom_name"
(IFS=.; echo "${labels[*]:0:4}")