Perl脚本在分隔符后将多行连接成单行

时间:2012-11-09 04:54:26

标签: perl

我从远程系统收到消息,响应是多行的,我必须根据分隔符将其转换为单行。

流阅读器的内容如下所示

Iam in first Line
Iam in second Line
:

Iam in third Line
Iam  in the forth Line
Iam in fifth Line
:

Iam in Sixth Line
Iam In seventh Line
IAm in Eighth Line
:

应将多行响应转换为单行,直到分隔符“:”

请注意我从远程系统读取它,而不是从文件中读取它,它连续并且没有eof.until与远程系统的连接终止

输出应为:

Iam in first Line Iam in second Line:
Iam in third Line Iam in the forth Line:
Iam in Sixth Line Iam In Seventh Line Iam in Eighth Line:

有人可以帮助一个方法或命令来实现这个目标吗?

2 个答案:

答案 0 :(得分:3)

一种方式:

$ perl -pne 'chomp unless(/^:/);' file
Iam in first LineIam in second Line:
Iam in third LineIam  in the forth LineIam in fifth Line:
Iam in Sixth LineIam In seventh LineIAm in Eighth Line:

答案 1 :(得分:-1)

perl -p -e 's/\n/ /g;s/:[\s]*/:\n/g' your_file

测试如下:

> cat temp
Iam in first Line
Iam in second Line
:

Iam in third Line
Iam  in the forth Line
Iam in fifth Line
:

Iam in Sixth Line
Iam In seventh Line
IAm in Eighth Line
:
> perl -p -e 's/\n/ /g;s/:[\s]*/:\n/g' temp
Iam in first Line Iam in second Line :
 Iam in third Line Iam  in the forth Line Iam in fifth Line :
 Iam in Sixth Line Iam In seventh Line IAm in Eighth Line :
>