如何将(“)字符未启动的新行更改为另一个字符串

时间:2016-10-14 14:12:55

标签: regex string sed

我需要将"(引用)未启动的换行符更改为其他可打印字词,例如\n<br>

我试过这个,但它不起作用:

cat file.csv | sed 's/^[^\"]/\<br\>/g'

输入文件的示例:

cat file.csv

"a","bcde","fgh
ijk
   mnopq
asd"

我需要的输出:

cat file.csv

"a","bcde","fgh<br>ijk<br>   mnopq<br>asd"

2 个答案:

答案 0 :(得分:0)

您可以在sed中使用条件分支:

sed -i -E ':a;N;s~\n([^"])~<br\>\1~;ba' file.csv

# check results    
cat file.csv

"a","bcde","fgh<br>ijk<br>   mnopq<br>asd"

Read more about it

答案 1 :(得分:0)

我不认为定位一条没有双引号的换行符是一种可行的方式来做你想做的事情。例如,它不会处理像这样的情况:

"abc","def
"

更可靠的方法是检查一行中是否有奇数个双引号并附加下一行直到该数字变为偶数,然后您可以继续进行替换:

sed -E '/^("[^"]*"[^"]*)*"[^"]*$/{:a;N;/^("[^"]*"[^"]*)*$/{s/\n/<br>/g;bb};ba;};:b;' file

-E将正则表达式语法切换为ERE(扩展正则表达式)
-i就地更改文件内容(如果您确定,请添加此开关)

命令详情:

/^("[^"]*"[^"]*)*"[^"]*$/ # check if the line has an odd number of quotes
{ # when the match succeeds:
    :a; # define a label "a"
    N;  # append the next line to the pattern space
    /^("[^"]*"[^"]*)*$/ # check if the pattern space contains an even number of quotes
    { # in this case:
       s/\n/<br>/g; # proceed to the replacement
       bb; # go to label "b"
    };
    ba; # go to label "a"
};
:b; # define the label "b"