sms; deliver;“+ 99999999999”;“”;“”;“2012.06.23 09:21”;“”;“xxxxxxxxxxxxx XXXXXXXX,
XXXXXXXXXXXX
XXXXXXXXXXXX
XXXXXXXXXXXX“
我需要任何不以“sms; deliver;”开头的行要添加到上一行。即获得这样一条线:
sms; deliver;“+ 99999999999”;“”;“”;“2012.06.23 09:21”;“”;“xxxxxxxxxxxxx xxxxxxxx,xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxx“
^这是一行。另外,删除/替换xxxxx(内容)部分中的任何双引号会很有帮助。
sms; deliver;“+ 99999999999”;“”;“”;“2012.06.23 09:21”;“”;“xxxxxxxxxxxxx xxxxxxxx,xxxxxxxxxxxx xxxxxxxxxxxx”xxxx“xxxxxxxx”
因此上面的行将转换为此(双引号转换为单引号):
sms; deliver;“+ 99999999999”;“”;“”;“2012.06.23 09:21”;“”;“xxxxxxxxxxxxx xxxxxxxx,xxxxxxxxxxxx xxxxxxxxxxxx'xxxx'xxxxxxxx”
答案 0 :(得分:3)
以下sed
命令似乎可以满足你的需要(编辑:开头的一个简短的sed
命令来过滤引号):
sed '/^sms;deliver;/!'"y/\"/'/" yourfile | sed -n '/^sms;deliver;/!b;:r;${p;b};N;/\nsms;deliver;/!{s/\n//;br};P;s/.*\n//;br'
一个简短的解释:
sed -n '# not print by default
/^sms;deliver;/!b # if line not starting with the pattern, goto end
:r #label r
${p;b} # if last line, print & exit
N # read new line, append to pattern space
/\nsms;deliver;/!{s/\n//;br} # if appended line doesn't start with pattern,
# remove newline & goto r
P # print everything up to the newline
s/.*\n//;br # remove what was just printed, goto r'
开头的sed
只有在"
'
更改为sms;delivered;
答案 1 :(得分:2)
这可能对您有用:
sed ':a;$!N;/\nsms;deliver;/!s/\n//;ta;:b;s/\(;".*\)"\([^";]*\)"\([^";]*"\)$/\1'\''\2'\''\3/;tb;P;D' file
编辑:
"
问题的测试数据:
echo 'sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx, xxxxxxxxxxxx xxxxxxxxxxxx "xxxx"xxxxxxxx"' >/tmp/a
sed ':a;$!N;/\nsms;deliver;/!s/\n//;ta;:b;s/\(;".*\)"\([^";]*\)"\([^";]*"\)$/\1'\''\2'\''\3/;tb;P;D' /tmp/a
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx, xxxxxxxxxxxx xxxxxxxxxxxx 'xxxx'xxxxxxxx"
sed 's/xx/"&"/g' /tmp/a >/tmp/b
sed ':a;$!N;/\nsms;deliver;/!s/\n//;ta;:b;s/\(;".*\)"\([^";]*\)"\([^";]*"\)$/\1'\''\2'\''\3/;tb;P;D' /tmp/b
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"'xx''xx''xx''xx''xx''xx'x 'xx''xx''xx''xx', 'xx''xx''xx''xx''xx''xx' 'xx''xx''xx''xx''xx''xx' ''xx''xx'''xx''xx''xx''xx'"