
輸入檔案具有兩個記錄和三個字段,第二個屬性包含換行符。
輸入檔:
100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24
所需的輸出應如下所示
"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
答案1
你可以嘗試使用這個 awk :
awk -F'\n' '!f{$1="\""$1;f=1}{f=f+gsub("[|]","\"|\"")}f==3{$0=$0"\"";f=0}1' infile
答案2
你可以這樣做perl
:
perl -0pe 's/([^|]*)\|([^|]*)\|([^|\n]*)(\n|$)/"\1"|"\2"|"\3"\4/g' input_file
-0
一次讀取文件,而不是逐行讀取。-p
列印最後一行-e
表達方式s/pattern/replacement/g
用替換替換圖案
答案3
$ cat input.txt | tr "\n" "\t" \
|awk -v FS="|" -v OFS="|" '{for(i=1;i<=NF;i++) $i="\""$i"\"";}1' \
|tr "\t" "\n"
tr
將新行轉換為選項卡。所以輸入是線性化的- 我們告訴
awk
將 處理|
為字段分隔符號。現在我們可以迭代每個欄位並添加引號。然後列印整行 tr
將製表符轉換回新行