给定一个IP地址192.168.10.21.somebody.com.br,我只需要提取192.168.10.21--我在下面尝试过--,它给出了“剪切:无效字节或字段列表”。
切-d'.‘-f-4
发布于 2015-08-08 14:26:20
$ echo "192.168.10.21.somebody.com.br" | cut -d'.' -f -4
192.168.10.21为我工作!
发布于 2015-08-08 16:05:03
以下三项假设您的域名存储在一个参数中
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}")https://stackoverflow.com/questions/31894000
复制相似问题