如何使用 awk 或 sed 从列中删除 \n

如何使用 awk 或 sed 从列中删除 \n

我有数据:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse
1) Lorem ipsum dolor sit amet, consectetur adipiscing elit
2) Lorem ipsum dolor sit amet, consectetur adipiscing elit
3) Lorem ipsum dolor sit amet, consectetur adipiscing elit

Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

如果可能的话,我想使用 sed 或 awk 删除第 4 列中的 \n 。结果看起来像这样:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

\n 之前的模式总是不同的,可以是 1-9 或文本,也可以是:, ;, (,等符号)

文本已更改,但格式与原始数据相同。

答案1

sed- 如果该行仅包含非管道字符,则连接到上一行:

sed -e :a -e '$!N; s/\n\([^|]*\)$/ \1/; ta' -e 'P;D' file
1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit  Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

答案2

Awk解决方案:

awk -F'|' '{ printf "%s%s", (NR == 1? "" : (NF > 1? ORS : OFS)), $0 }
           END{ print "" }' file

输出:

1528198031|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528202225|Andr|thund|Lorem ipsum dolor sit amet, conse 1) Lorem ipsum dolor sit amet, consectetur adipiscing elit 2) Lorem ipsum dolor sit amet, consectetur adipiscing elit 3) Lorem ipsum dolor sit amet, consectetur adipiscing elit  Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit
1528202574|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203825|Andr|thund|Lorem ipsum dolor sit amet, consectetur adipiscing elit
1528203890|Andr|rail|Lorem ipsum dolor sit amet, consectetur adipiscing elit

相关内容