Skip to content Skip to sidebar Skip to footer

How To Extract The Data Using Sed Command

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&PerMa

Solution 1:

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.

Solution 2:

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.

Post a Comment for "How To Extract The Data Using Sed Command"