尝试将下一行连接到sed中以反斜杠结尾的行

时间:2013-11-26 09:41:01

标签: shell scripting sed

有两个文件file.txt和file1.txt

$ cat file.txt
Linux
Administration
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online\
Security
Productivity\
Google Search\
Tips
"Web Based Time Tracking,
Web Based Todo list and
Reduce Key Stores etc"
$

$ cat file1.txt

AIX
Solaris\
Unix\ 
Linux 
HPUX

我正在尝试将下一行追加到以反斜杠结尾的行,并将上述文件作为输入。

file.txt中的o / p如下所示,它是正确的

$ sed ':loop /\\$/N; s/\\\n */ /; tloop' file.txt
Linux
Administration
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online Security
Productivity Google Search Tips
"Web Based Time Tracking,
Web Based Todo list and
Reduce Key Stores etc"
$

如果我再次使用file1.txt作为输入在行上方运行,则o / p不正确

$ sed ':loop /\\$/N; s/\\\n */ /; tloop' file1.txt
AIX
Solaris Unix\ 
Linux 
HPUX

任何人都可以解释为什么file1.txt没有显示正确的o / p。

2 个答案:

答案 0 :(得分:1)

这可能适合你(GNU sed):

sed ':a;$!N;s/\\\s*\n\s*/ /;ta;P;D' file

这会占用换行符两侧的空格。

答案 1 :(得分:0)

反斜杠后面有空格。尝试:

sed ':loop /\\ *$/N; s/\\ *\n */ /; tloop' file1.txt