regex - What does the following SED pattern exactly do? -


i working on cgi script , developer worked on before me has used sed pattern.

command=`echo "$query_string" | sed -n 's/^.*com_tex=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"` 

here com_tex name of text box in html.

what line takes value form html text box , assigns shell variable. sed pattern apparently (not sure) necessary extract value html without other unnecessary accompanying stuff.

i mention issue asking this. same pattern used text area entering command , need retrieved is. it's getting jumbled up. eg. if enter following command in text box:

/usr/bin/free -m >> /home/admin/memlog.txt

the value gets stored in variable is:

%2fusr%2fbin%2ffree+-m+%3e%3e+%2fhome%2fadmin%2fmemlog.txt

all of can / being substituted %2f, space + , > sign %3e.

but can not figure how specified in above pattern! please tell me how pattern works or pattern should substitute there entered command instead of output getting?

 sed -n 

-n switch means "dont print"

's/ 

s substitutions, / delimiter command looks like
s/thing sub/subsitution/optional command

^.*com_tex= 

^ means start of line
.* means match 0 or more of character
match longest string start of line com_tex=

\(\) 

this capture group, whatever matched inside these brackets saved , can used later

[^&]* 

[^] when hat used inside square brackets means not match characters inside brackets
* same before means 0 or more matches

the capture group combined means capture character except &.

 .*$ 

the same first bit except $ means end of line, matches until end

/\1/p'  

after second / substitution. \1 capture group before, substitute matched in first part(the whole line) capture group. p means print, must explicitly stated -n switch used , prevent other lines being printed.

| 

pipe

s/%20/ /g 

sub %20 space, g means global every match on line

hth :)


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -