0

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*
Djidiouf
  • 191

2 Answers2

1

why don't you replace the *** or +++ to some other symbol then use that symbol as separator

#!/bin/bash
input=$1

formatted_input=$(echo ${input} | sed "s/\*\*\*/\|/g;s/+++/\|/g")

field2=$(echo "${formatted_input}"  | awk -F\| '{print $2}')
field3=$(echo "${formatted_input}" | awk -F\| '{print $3}')
echo "field2=$field2"
echo "field3=$field3"

try this...

$ echo "***test hello***hi test msg*this***" | awk -vFS='\\*\\*\\*' '{print "Field 2 : "$2;print "Field 3 : "$3}'
Field 2 : test hello
Field 3 : hi test msg*this
Kamaraj
  • 4,365
  • Yes that's a possibility but I wanted to have a more direct approach. I think that is something achievable with awk but can't figure how. I tried to escape the * and even to double escape them but without success in the output. – Djidiouf Oct 27 '16 at 04:04
  • modified the answer with FS variable. try that... – Kamaraj Oct 27 '16 at 05:11
  • the single quotes were what I missed in my tries, thank you – Djidiouf Oct 27 '16 at 05:59
1
#!/bin/bash

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"

$ ./parse_options.sh "***sub  option*** Main option, still*bla*"
field2=sub  option
field3= Main option, still*bla*


Further reading:

Sundeep
  • 12,008