
入力ファイルには 2 つのレコードと 3 つのフィールドがあり、2 番目の属性には改行文字が含まれています。すべてのフィールド値を二重引用符で囲みます。
入力ファイル:
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
タブを新しい行に戻す