shell - Delete First and Second Period While Keeping Everything in Between (Linux) -
user calls script along input xx.xx.xx.xx x's numbers. i'm trying extract second set of xx left.
i've looked @ examples doing:
${1%.*} <- deletes last . , else after ${1##*.} <- deletes last . , before but without explanation on special characters do, i'm having trouble solving problem.
any appreciated.
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
Comments
Post a Comment