In bash, how do I get a substring of everything from the first occurrence of a character to the second occurrence of the same character.
Example...
Input String = "abc-def-ghi"
Character = "-"
Desired Output String = "def"
In bash, how do I get a substring of everything from the first occurrence of a character to the second occurrence of the same character.
Example...
Input String = "abc-def-ghi"
Character = "-"
Desired Output String = "def"
On
Let's say you have:
s="abc-def-ghi"
ch='-'
Using BASH read builtin:
IFS="$ch" read -ra arr <<< $s && echo "${arr[1]}"
Or, using BASH regex:
re="$ch([^$ch]*)$ch"
[[ $s =~ -([^-]*)- ]] && echo "${BASH_REMATCH[1]}"
Output:
def
Possible use awk with
-delimiter-F - what field separator to use.
{print $2} - print second position