我正在尝试编写一个命令(sed / awk),用于在以下条件下用短划线替换换行符:
这不应该用破折号替换,因为CSV中没有新行:
X00000;111111;1111111111;This is just a text
无论如何,在这个例子中,新行应该用短划线代替:
X00000;111111;1111111111;This is a longer text which contains a
new line sign.
替换的输出应如下所示:
X00000;111111;1111111111;This is a longer text which contains a - new line sign.
编辑:这也适用于这样的行:
X00000;111111;1111111111;"This is a longer text which contains a
new line sign
or even more
or a line that even contains only a new line sign
"
在这种情况下,需要以下输出:
X00000;111111;1111111111;"This is a longer text which contains a - new line sign - or even more - - or a line that even contains only a new line sign - "
答案 0 :(得分:1)
使用awk你可以这样做:
awk -F ';' 'NF<4{print p, "-", $0;p="";next} p{print p} {p=$0} END{if (p) print p}' file.csv
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a - new line sign.
答案 1 :(得分:1)
以下是使用sed
的选项:
$ cat file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a
new line sign.
X00000;111111;1111111111;"This is a longer text which contains a
new line sign
or even more
or a line that even contains only a new line sign
"
$ sed ':a;$bc;N;s/\n/ - /;ba;:c;s/ - X00000;/\nX00000;/g' file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a - new line sign.
X00000;111111;1111111111;"This is a longer text which contains a - new line sign - or even more - - or a line that even contains only a new line sign - - "
<强>解释强>
sed '
:a # Create a label a
$bc # If it is last line, branch to label c
N # Append next line to pattern space
s/\n/ - / # Remove the \n and replace it with -
ba # Keep repeating above steps until file is complete
:c # Our label c. Do the following when end of file is reached
s/ - X00000;/\nX00000;/g # We do this substitution to add newlines where needed.
' file
答案 2 :(得分:0)
这是一个awk
,只是加在一起。
awk '{printf (NR==1||!/X00000/)?$0:RS $0} END {print ""}' file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a new line sign.
X00000;111111;1111111111;"This is a longer text which contains anew line signor even moreor a line that even contains only a new line sign"
不会添加-