简体   繁体   English

使用SED:查找正则表达式>替换为匹配组

[英]Using SED: Find Regex > Replace With Match Groups

So I'm trying to restructure an access log to a particular format, I've already managed it in grok but learning sed sounds very useful so please humour me: 所以我正在尝试将访问日志重新构建为特定格式,我已经在grok中管理它但是学习sed听起来非常有用所以请幽默我:

So, using SED I'm trying to find: 所以,使用SED我试图找到:

(\d+\.\d+\.\d+\.\d+) - - \[(.*?)\] "(\w{3,4}) (\/.*?(\/|\.\w+)) (HTTP(S)?\/.*?)"(\d{3}) (\d+) "(.*?)" "(.*?)" "(.*?), (.*?)"(\[.*?\]) (\[.*?\])

And Replace with: 并替换为:

$12 - - [\2] "\3 \4" \8 \9 "$10" "$11"

Match in File(A) replaced lines sent to File(B) 匹配文件(A)替换发送到文件(B)的行

I've tried: 我试过了:

sed -r -i 's/((\d+\.\d+\.\d+\.\d+) - - \[(.*?)\] "(\w{3,4}) (\/.*?(\/|\.\w+)) (HTTP(S)?\/.*?)"(\d{3}) (\d+) "(.*?)" "(.*?)" "(.*?), (.*?)"(\[.*?\]) (\[.*?\]))/$12 - - [\2] "\3 \4" \8 \9 "$10" "$11"/g;' fileA.txt > fileB.txt

To which it (GNU) throws the following error: 它(GNU)引发以下错误:

sed: -e expression #1, char 1: unknown command: `''

I'm afraid I'm totally new to sed so this is as far as my Googlefu has gotten me. 我担心我对sed完全陌生,所以这就是我的Googlefu给了我的。

Input: 输入:

IPAddress1 - - [30/Mar/2017:11:33:55 +0100] "GET /image.jpg HTTP/1.1 "200 2607 "http://www.example.co.uk/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "IPaddress2, IPAddress 3"[abc] [def]

Expected Output: 预期产出:

IPAddress2 - - [30/Mar/2017:11:33:55 +0100] "GET /image.jpg" 200 2607 "http://www.example.co.uk" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"

Thank you kindly. 非常感谢你。

You need to make same changes in our regex to run it with sed. 您需要在我们的正则表达式中进行相同的更改才能使用sed运行它。

I recommend you to use the -r parameter to can use extended regex syntax , it is more easy for JavaScript|PHP|Java users. 我建议你使用-r参数来使用扩展的正则表达式语法 ,它对JavaScript | PHP | Java用户来说更容易。

Here is a little peace of your regex working: 这是你的正则表达式工作的一点点安宁:

echo \
'127.0.0.1 - - [30/Mar/2017:11:33:55 +0100] "GET /image.jpg HTTP/1.1 "200 2607 "http://www.example.co.uk/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "IPaddress2, IPAddress 3"[abc] [def]' \
| sed -r 's/([0-9]+(\.[0-9]+)+) - - \[([0-9]+)\/([^\/]+)\/([0-9]+):/"\1", "\3", "\4", "\5"/g'

This example is not completed , it only for show you the correct syntax . 此示例未完成 ,它仅用于显示正确的语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM