為了對各種段落進行排序(abc order),我嘗試了:
awk 'BEGIN { RS="" } { a[FNR]=$0 } END { PROCINFO["sorted_in"]="@val_str_asc" for (i in a) print a[i] ORS } ' myrecords.txt
但它不會排序。樣本記錄:
Ham
this is good
(mind the mail)
Cheese
I'm fine
Turkey
(empty)
文本塊可能有一行或多行,由一個或多個空白行分隔,甚至是日期而不是空白。后者可以通過用空行替換日期來解決。
期望的結果:
Cheese
I'm fine
Ham
this is good
(mind the mail)
Turkey
(empty)
uj5u.com熱心網友回復:
從您的評論中顯示的輸出中,您的行都以 control-Ms ( Carriage Returns) 結尾,因此看起來為空的行實際上并非如此,因此當 RS 為空時,您的整個檔案是一條記錄。運行dos2unix或sed 's/\r$//'在您的輸入檔案上洗掉這些CRs,然后再次運行 awk 命令。在我對輸入運行 sed 以洗掉 CR 之前和之后,請參閱以下差異:
$ cat -Ev file
Ham ^M$
this is good ^M$
(mind the mail)^M$
^M$
Cheese ^M$
I'm fine^M$
^M$
Turkey^M$
(empty)^M$
$ awk -v RS= '{print NR, "<" $0 ">"}' file | cat -Ev
1 <Ham ^M$
this is good ^M$
(mind the mail)^M$
^M$
Cheese ^M$
I'm fine^M$
^M$
Turkey^M$
(empty)^M>$
$ sed 's/\r$//' file > tmp && mv tmp file
$ cat -Ev file
Ham $
this is good $
(mind the mail)$
$
Cheese $
I'm fine$
$
Turkey$
(empty)$
$ awk -v RS= '{print NR, "<" $0 ">"}' file | cat -Ev
1 <Ham $
this is good $
(mind the mail)>$
2 <Cheese $
I'm fine>$
3 <Turkey$
(empty)>$
請參閱為什么我的工具輸出會覆寫自身以及如何修復它?有關這些 DOS 行結尾的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/434636.html
