1

so for example, I have a string like this:

f=awerawet1351&q=aet12462awera&l=3ewar462&h=awiejt361

and I wanna grab the substring between "q=" and "&", which is "aet12462awera" or "q=aet12462awera" is also fine. I tried to useawk -F'&' , but the thing is, the position for "q="string is not fixed. Sometimes, I will have

f= .... & l=.... &q=....& h=....

so I can't use awk -F'&' '{print $2}' to get the result.

Is there anyway for me to use unix command to get it? Thanks!

jxymixer
  • 27
  • 3

2 Answers2

1

This seems to work:

STRING='f=awerawet1351&q=aet12462awera&l=3ewar462&h=awiejt361'
echo $STRING | awk -F"q=" '{print $2}' | cut -f1 -d"&"
nando
  • 501
  • 5
  • 6
0
string='f=awerawet1351&q=aet12462awera&l=3ewar462&h=awiejt361'
IFS='&'
for field in $string; do
    if [ "${field%=*}" = "q" ]; then
        echo ${field#*=}
    fi
done

Look at parameter expansion and IFS.

jvdm
  • 846
  • 6
  • 22