how to extract the data using sed command
Asked Answered
U

2

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.

Unimpeachable answered 10/6, 2013 at 10:22 Comment(0)
S
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.

Snotty answered 10/6, 2013 at 10:32 Comment(4)
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.125Unimpeachable
I have tried but the result was NULL it was not printing any thingUnimpeachable
@amar: It does not print anything. It assigns the variables. You have to add echo $mac and similar to get the output.Snotty
I Got the out put by using user2234712's suggestion thanks for your helpUnimpeachable
M
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.

Meimeibers answered 10/6, 2013 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.