为什么我的 AWK 函数只能在我的文件的缩短版本上工作
Is there any reason why my AWK functions work only on a shortened version of my file
我有一个简单的 AWK 函数:
awk '
BEGIN { FS=" "; RS="\n\n" ; OFS="\n"; ORS="\n" }
/ms Response/ { print [=10=] }
' $FILE
FILE 是一个大型日志,包含如下部分:
2021-10-13 12:15:12 CDT 526ms Request
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: xxxxxxxxxxxxxxxxxxx
Content-Length: 279
<query xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><product><name>drill</name><price>99</price><stock>5</stock></product>/query>
2021-10-13 12:15:12 CDT 880ms Received
2021-10-13 12:15:12 CDT 896ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 472
<?xml version="1.0"?>
<query type="c" xmlns="xxxxxxxxxxxxxx">
<product>
<name>screwdriver</name>
<price>5</price>
<stock>51</stock>
</product>
</query>
2021-10-13 12:15:12 CDT 947ms Request
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: xxxxxxxxxxxxxxx
Content-Length: 515
Expect: 100-continue
以上只是一个片段,文件继续超过 14000 行,重复相同的模式。
现在,当我 运行 我的 AWK 函数处理整个文件时,它只是 returns 整个文件返回。但是当我 运行 它在一个用 (cat $FILE | head -200) 创建的文件上时,它按预期返回:
2021-10-13 12:15:12 CDT 896ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 472
2021-10-13 12:15:13 CDT 075ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 3207
2021-10-13 12:15:13 CDT 208ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 4220
为什么我可以 运行 这个在一个缩短的文件上,但是当我 运行 它在一个较长的版本上时,它不起作用?即使文件中的数据相同?
我正在 Bash 中开发 Ubuntu 18.04 LTS。
谢谢!
@markp-fuso 的评论对我有帮助。我的输入文件有 Windows 行结尾,我只需要 运行 在执行 AWK 之前执行以下命令:
tr -d '' < OGfile.txt > unixFile.txt
然后它 运行 正如预期的那样。
我从以下问题中获得了额外的语法帮助:Convert line endings
你可以使用这个:
awk -v RS= -v ORS='\n\n' '/ms Response/'
或者这样,以避免尾随空行:
awk -v RS= '/ms Response/ && c++ {printf "\n"} /ms Response/'
如果 RS
是一个空字符串,记录分隔符将变为两个或多个连续的新行。
我有一个简单的 AWK 函数:
awk '
BEGIN { FS=" "; RS="\n\n" ; OFS="\n"; ORS="\n" }
/ms Response/ { print [=10=] }
' $FILE
FILE 是一个大型日志,包含如下部分:
2021-10-13 12:15:12 CDT 526ms Request
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: xxxxxxxxxxxxxxxxxxx
Content-Length: 279
<query xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><product><name>drill</name><price>99</price><stock>5</stock></product>/query>
2021-10-13 12:15:12 CDT 880ms Received
2021-10-13 12:15:12 CDT 896ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 472
<?xml version="1.0"?>
<query type="c" xmlns="xxxxxxxxxxxxxx">
<product>
<name>screwdriver</name>
<price>5</price>
<stock>51</stock>
</product>
</query>
2021-10-13 12:15:12 CDT 947ms Request
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: xxxxxxxxxxxxxxx
Content-Length: 515
Expect: 100-continue
以上只是一个片段,文件继续超过 14000 行,重复相同的模式。
现在,当我 运行 我的 AWK 函数处理整个文件时,它只是 returns 整个文件返回。但是当我 运行 它在一个用 (cat $FILE | head -200) 创建的文件上时,它按预期返回:
2021-10-13 12:15:12 CDT 896ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 472
2021-10-13 12:15:13 CDT 075ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 3207
2021-10-13 12:15:13 CDT 208ms Response
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 4220
为什么我可以 运行 这个在一个缩短的文件上,但是当我 运行 它在一个较长的版本上时,它不起作用?即使文件中的数据相同?
我正在 Bash 中开发 Ubuntu 18.04 LTS。
谢谢!
@markp-fuso 的评论对我有帮助。我的输入文件有 Windows 行结尾,我只需要 运行 在执行 AWK 之前执行以下命令:
tr -d '' < OGfile.txt > unixFile.txt
然后它 运行 正如预期的那样。
我从以下问题中获得了额外的语法帮助:Convert line endings
你可以使用这个:
awk -v RS= -v ORS='\n\n' '/ms Response/'
或者这样,以避免尾随空行:
awk -v RS= '/ms Response/ && c++ {printf "\n"} /ms Response/'
如果 RS
是一个空字符串,记录分隔符将变为两个或多个连续的新行。