Pedro Caldeira
Pedro Caldeira

Reputation: 45

Search pattern to a special character with awk

I have a file like the one below

HTTP/1.1 401 Unauthorized
Server: WinREST HTTP Server/1.0
Connection: Keep-Alive
Content-Type: text/html
Content-Length: 89
WWW-Authenticate: ServiceAuth realm="WinREST", nonce="1828HvF7EfPnRtzSs/h10Q=="

<html><head><title>Unauthorized</title></head><body>Error 401: 
Unauthorized</body></html>

I need to get the nonce, that is just the 1828HvF7EfPnRtzSs/h10Q== in front og the word nonce=

I was using

grep -oP 'nonce="\K[^"]+' response.xml 

but the P parameter no longer works. how can I do the same with awk or even Grep with another parameter, maybe ?

Thanks in advance

Upvotes: 0

Views: 510

Answers (2)

karakfa
karakfa

Reputation: 67507

with sed

$ sed -nE 's/.*nonce="([^"]+)"/\1/p' file

1828HvF7EfPnRtzSs/h10Q==

with grep pipeline

$ grep -oE 'nonce=\S+' file | cut -d= -f2- | tr -d '"'

1828HvF7EfPnRtzSs/h10Q==

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133538

Solution 1st: Following awk may help you on same.

awk -F"\"" '/nonce/{print $(NF-1)}' Input_file

Solution 2nd: A sed solution too for same.

sed -n '/nonce=/s/\(.*nonce\)="\([^"]*\)\(.*\)/\2/p'  Input_file

Output will be 1828HvF7EfPnRtzSs/h10Q== in both codes above.

Upvotes: 1

Related Questions