0

I need to parse the output of a curl command:

curl --user test:test http://192.168.1.1/security/pkm.html | egrep '@company|config.pkm.password'

This returns:

<input type="text" id="config.pkm.dentity" name="config.pkm.identity" value="[email protected]" maxlength="64" />
<input type="text" id="config.pkm.inner_identity" name="config.pkm.inner_identity" value="[email protected]" maxlength="64" />
<input type="password" id="config.pkm.password" name="config.pkm.password" value="382738" maxlength="64" />

I want to search for name="config.pkm.identity" and print [email protected], for name="config.pkm.inner_identity" and print [email protected], and for name="config.pkm.password" and print 382738

Grep outputs just [email protected], [email protected] and 382738.

1

2 Answers 2

1

You really should use a HTML parser for this, but a (fragile) Awk solution would be:

 awk -F'"' '/pkm.identity/ {id = $8}; /inner_/ {inner = $8}; /password/ {pass = $8} END {print id" "inner" "pass}' file
 [email protected] [email protected] 382738
3
0

To grab information in the way of query you should use regex capture groups which are absent in grep. So try to filter out the curl output with sed (or awk) command:

sed -n 's/.*name="config.pkm.identity" value="\(.[^"]*\)".*$/\1/p'

where value of the field value is captured in \1 (regex capture group #1). This will output the value of name field is config.pkm.identity.

For name is config.pkm.password use:

sed -n 's/.*name="config.pkm.password" value="\(.[^"]*\)".*$/\1/p'

and etc.

To show all available values of corresponding names just use:

sed -n 's/.*name=".*" value="\(.[^"]*\)".*$/\1</p'

 

UPDATE for comment

To make choice of the matching values in a sed's query use the following scheme: Use additional regex grouping with | pipe sign which means OR statement. That allows sed to make choice from given set of variants to match final results (also note to escape both | pipe sign and ( ) parentheses)

For example:

sed -n -e 's/.*name="\(config.pkm.identity\|config.pkm.inner_identity\|config.pkm.password\)" value="\(.[^"]*\)".*$/\2/p'

this will search and output data for config.pkm.identity, config.pkm.inner_identity and config.pkm.password names in the stream.

Note also that the final regex capture group reference is \2 - it is the second group now.

1
  • i cant use last sed you provide cause i have many names on my curl and i just want 3 of themes , how can i use 2 sed you provided in one command ? Mar 31, 2016 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .