I try to parse my input string in a script with awk
and I have encountered some limitations with multiple special characters like ***
and +++
.
However, with the same script, with :::
or ---
as delimiters, I do not have any issue.
My script:
input=$1
delimiter=":::"
field2=$(echo "$input" | awk -F"$delimiter" '{print $2}')
field3=$(echo "$input" | awk -F"$delimiter" '{print $3}')
echo "field2=$field2"
echo "field3=$field3"
Output with :::
as a delimiter:
bash-3.2$ ./parse_options.sh ":::sub option::: Main option, still:bla:"
field2=sub option
field3= Main option, still:bla:
Now if I try to use ***
as a delimiter but have other isolated *
in my string, here is what I get unfortunately: the *bla*
is counted as an another field and that's not what I want:
bash-3.2$ ./parse_options.sh "***sub option*** Main option, still*bla*"
field2=sub option
field3= Main option, still
As you can see, *bla*
do not appear in the third field, the delimiter set in awk
is not respected in that case.
And it is the same thing with +++
as a delimiter:
bash-3.2$ ./parse_options.sh "+++sub option+++ Main option, still+bla+"
field2=sub option
field3= Main option, still
For further clarifications :
input=***sub option*** Main option, still*bla*
Expected output=
field2=sub option
field3= Main option, still*bla*
delimiter='\\*\\*\\*'
... note the use of single quotes – Sundeep Oct 27 '16 at 03:58