In this tutorial, How to extract substring in Bash Shell on Linux. You can use a select substring from the string. The awk command has a few useful functions. It is
Function substr mean Returns a substring.
The syntax extract substring in Bash Shell
substr(S,P,N)
- P: starting at position
- N: it
is returns N number ofcharacter’s from string S.
For example, I will use a string “HuuPV2123456789” with a length is 15 and starting position is 10.
$ echo "HuuPV2123456789" | awk '{ printf "%s", substr($0, 10, (length($0)-9)) }'
The result is below
456789
man page function substr here. Thank you for reading the DevopsRoles page!
$ VAR=HuuPV2123456789
$ echo ${VAR:9}
456789
$ echo ${VAR:9:3}
456
$ echo ${VAR:9:-1}
45678