我有一個簡單的 AWK 函式:
awk '
BEGIN { FS=" "; RS="\n\n" ; OFS="\n"; ORS="\n" }
/ms Response/ { print $0 }
' $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 函式時,它只會回傳整個檔案。但是當我在用 (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。
謝謝!
uj5u.com熱心網友回復:
@markp-fuso 的評論幫助了我。我的輸入檔案有 Windows 行結尾,我只需要在執行 AWK 之前運行以下命令:
tr -d '\15\32' < OGfile.txt > unixFile.txt
然后它按預期運行。
我從以下問題中獲得了額外的語法幫助:轉換行尾
uj5u.com熱心網友回復:
你可以使用這個:
awk -v RS= -v ORS='\n\n' '/ms Response/'
或者,為了避免尾隨空行:
awk -v RS= '/ms Response/ && c {printf "\n"} /ms Response/'
如果RS是空字串,則記錄分隔符變為兩個或多個連續的新行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/387554.html
