Add lines not beginning with specific regex to previous line
Pregunta
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx,
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx"
I need any lines that don't begin with "sms;deliver;" to be added to the previous line. i.e to get such a line :
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx, xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxx"
^ That is a single line. Also it would be helpful to remove/replace any double quotes in the xxxxx(content) part.
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx, xxxxxxxxxxxx xxxxxxxxxxxx "xxxx"xxxxxxxx"
So the above line would get converted to this(double quotes converted to single quotes):
sms;deliver;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxxx xxxxxxxx, xxxxxxxxxxxx xxxxxxxxxxxx 'xxxx'xxxxxxxx"
Solución
The following sed
command seems to do what you need (edited: a short sed
command in the beginning to filter quotes):
sed '/^sms;deliver;/!'"y/\"/'/" yourfile | sed -n '/^sms;deliver;/!b;:r;${p;b};N;/\nsms;deliver;/!{s/\n//;br};P;s/.*\n//;br'
A short explanation:
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'
The sed
in the beginning only changes "
to '
when it's not on a line with sms;delivered;
Otros consejos
This might work for you:
sed ':a;$!N;/\nsms;deliver;/!s/\n//;ta;:b;s/\(;".*\)"\([^";]*\)"\([^";]*"\)$/\1'\''\2'\''\3/;tb;P;D' file
EDIT:
Test data for "
's issue:
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'"