
データ出力ファイルには、改行で区切られた複数行のレコードがあります。各行では、列は区切り文字で区切られます|
。
区切り文字の間の各列にある追加のスペース、タブ、改行をすべて置き換えます。各データ行は「&*」で終わります。
データ:
char1 | char2 |
char3|char 4 &*
char11
| char 12 |char13|char14 &*
char21 | char22 |char23| char24 &*
役割:
- その後、末尾のスペース/タブを削除する必要があります。
- 行が次に開始/終了する場合は、行をリンクしてください。
|
- 繰り返される空白を圧縮します。
- 「&*」の後の新しい行をスキップ
結果:
char1|char2|char3|char 4
char11|char 12|char13|char14
char21|char 22|char23|char24
私が今持っているコードはタブを交換するためのものです。新しい行と追加のスペースを持つように変更する必要があります。
sed -i 's/[ \t]\+|/|/g' DataStats0914.txt
答え1
GNUを使用するとsed
:
sed -E '
:a /\s*&\*\s*$/ !{ N; s/\n//; ta; };
# read "N"ext line and join (s/\n//) except "!" lines that ends with "&*" chars;
s/&\*//g;
# also remove these chars "&*" too
s/\s*\|\s*/|/g;
# remove whitespaces around "|" char as well
' <(tr -s '\t ' ' ' <infile)
以下のコメントなしのコマンド:
sed -E '
:a /\s*&\*\s*$/ !{ N; s/\n//; ta; };
s/&\*//g;
s/\s*\|\s*/|/g;
' <(tr -s '\t ' ' ' <infile)