删除第一个和第二个句点,同时保持一切(Linux)(Delete First and Second Period While Keeping Everything in Between (Linux))

用户调用脚本以及输入XX.XX.XX.XX ,其中X's是数字。 我正试图从左边提取第二组XX 。

我看过以下做的例子:

${1%.*} <- deletes the last . and everything else after ${1##*.} <- deletes last . and everything before

但是如果没有对特殊角色做什么的解释,我就无法解决我的问题。

任何帮助将不胜感激。

User calls script along with input XX.XX.XX.XX where X's are numbers. I'm trying to extract the second set of XX from the left.

I've looked at examples where doing:

${1%.*} <- deletes the last . and everything else after ${1##*.} <- deletes last . and everything before

But without an explanation on what the special characters do, I'm having trouble solving my problem.

Any help would be greatly appreciated.

最满意答案

使用awk:

x='12.34.56.78' awk -F '.' '{print $2}' <<< "$x" 34

使用纯BASH(数组):

n=$(IFS='.' read -ra arr <<< "$x" && echo "${arr[1]}") && echo "$n" 34

使用不带阵列的纯BASH 1:

y="${x#*.}" echo "${y%%.*}" 34

使用没有数组2的纯BASH:

n=$(IFS='.' && set -- $x && echo "$2") && echo "$n" 34

使用sed:

sed 's/^[^.]*\.\([^.]*\).*$/\1/' <<< "$x" 34

Using awk:

x='12.34.56.78' awk -F '.' '{print $2}' <<< "$x" 34

Using pure BASH (arrays):

n=$(IFS='.' read -ra arr <<< "$x" && echo "${arr[1]}") && echo "$n" 34

Using pure BASH without arrays 1:

y="${x#*.}" echo "${y%%.*}" 34

Using pure BASH without arrays 2:

n=$(IFS='.' && set -- $x && echo "$2") && echo "$n" 34

Using sed:

sed 's/^[^.]*\.\([^.]*\).*$/\1/' <<< "$x" 34

更多推荐