使用awk搜索模式到特殊字符

时间:2021-09-16 20:08:17

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=

我需要得到nonce,这只是1828HvF7EfPnRtzSs / h10Q ==在前面和单词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 ?

但P参数不再有效。我怎么能用awk甚至grep用另一个参数做同样的事情呢?

Thanks in advance

提前致谢

2 个解决方案

#1


1  

with sed

与sed

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

1828HvF7EfPnRtzSs/h10Q==

with grep pipeline

用grep管道

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

1828HvF7EfPnRtzSs/h10Q==

#2


1  

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

解决方案1:跟随awk可能会帮助你。

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.

在上述两个代码中输出将为1828HvF7EfPnRtzSs / h10Q ==。

#1


1  

with sed

与sed

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

1828HvF7EfPnRtzSs/h10Q==

with grep pipeline

用grep管道

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

1828HvF7EfPnRtzSs/h10Q==

#2


1  

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

解决方案1:跟随awk可能会帮助你。

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.

在上述两个代码中输出将为1828HvF7EfPnRtzSs / h10Q ==。