0

I have the data this

&mac=1E-30-6C-A2-47-5F&ip=172.16.1.127&msk=255.255.255.0&gw=172.16.1.1&pdns=0.0.0.0&sdns=0.0.0.0&Speed=0&PortNo=10001&PerMatFram=0&ComPort=0

I want to extract the data string and store it in a variable using sed commond like

ip=172.16.1.127
mac=xyz

How to use sed with the above string?

I have tried using like this

IP=`echo "$QUERY_STRING" | sed -n '/&ip=/,/&)/g'

but it is not giving any data.

Peter
  • 14,559
  • 35
  • 55
amar
  • 509
  • 3
  • 8
  • 17

2 Answers2

0

If you data do not contain any special characters, you can use the following:

eval $( echo "$QUERY_STRING" | sed 's/&/ /g' )

That would create variables directly from the query string as mac=1E-30-6C-A2-47-5F etc. Be careful, though, as an attacker might request a page with the following query string:

&IFS=.&PWD=/&UID=0

See also How to parse $QUERY_STRING from a bash CGI script.

Community
  • 1
  • 1
choroba
  • 231,213
  • 25
  • 204
  • 289
  • I dont want use array just directly I want to extract the value and assign it to a variable like as i said ip=172.16.1.125 – amar Jun 10 '13 at 10:46
  • I have tried but the result was NULL it was not printing any thing – amar Jun 11 '13 at 05:26
  • @amar: It does not print anything. It assigns the variables. You have to add `echo $mac` and similar to get the output. – choroba Jun 11 '13 at 05:55
  • I Got the out put by using user2234712's suggestion thanks for your help – amar Jun 11 '13 at 09:07
0

Probably easier to do it in two steps, first trimming the left side, and then the right side.

sed 's/.*mac=//' | sed 's/\&.*//'

This will:
Step 1:
 Replace anything up until (and including) "mac=" with nothing
Step 2:
 Replace anything after (and including) the first ampersand (&) it encounters with nothing.