数据输出文件有多行记录,并用换行符分隔。在每一行中,列之间用分隔符分隔|
。
尝试替换分隔符之间每列中的所有额外空格、制表符、换行符。每行数据以“&*”结尾
数据:
char1 | char2 |
char3|char 4 &*
char11
| char 12 |char13|char14 &*
char21 | char22 |char23| char24 &*
角色:
- 需要删除尾随空格/制表符;然后
- 如果行以 a 开头/结尾,则加入行
|
- 挤压重复的空格。
- 在“&*”之后跳过新行
结果:
char1|char2|char3|char 4
char11|char 12|char13|char14
char21|char 22|char23|char24
Code what I have now is for replacing tab, need to change this to have new line and extra space.
sed -i 's/[ \t]\+|/|/g' DataStats0914.txt
答案1
With 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)
comments-free command below:
sed -E '
:a /\s*&\*\s*$/ !{ N; s/\n//; ta; };
s/&\*//g;
s/\s*\|\s*/|/g;
' <(tr -s '\t ' ' ' <infile)