how to extract the data using sed command

孤街浪徒 提交于 2019-12-12 02:16:20

问题


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.


回答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.




回答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.



来源:https://stackoverflow.com/questions/17021640/how-to-extract-the-data-using-sed-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!