sed - 将不以特定正则表达式开头的行添加到上一行

标签 sed grep tr

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(内容)部分中的任何双引号也会有所帮助。

短信;发送;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxx xxxxxxxx,xxxxxxxxxxxx xxxxxxxxxxxx "xxxx"xxxxxxx"

因此上面的行将转换为这样(双引号转换为单引号):

短信;发送;"+99999999999";"";"";"2012.06.23 09:21";"";"xxxxxxxxxxxx xxxxxxxx,xxxxxxxxxxxx xxxxxxxxxxxx 'xxxx'xxxxxxx"

最佳答案

以下 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; 不在同一行时才将 " 更改为 '

关于sed - 将不以特定正则表达式开头的行添加到上一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169974/

相关文章:

linux - 更改大括号样式的 Bash 脚本

awk - 在文本文件的第一行和第二行之间添加一个空行

BASH:在文件中搜索几个关键字并突出显示它们

bash - 使用 sed 命令解析资源 Visual Studio .rc 文件

bash - 将文本文件第一行的日期 append 到每一行的开头

grep - 如何检查 Ansible 的命令输出中是否存在字符串列表?

linux - shell Linux : grep exact sentence with NULL character

Bash:带有表示字符的变量的 tr 命令

linux - bash 文本文件转置,添加新列并再次制作一个大的两列

bash - 使用 tr 将非字母字符替换为 '-' 会在字符串末尾添加一个额外的字符