Prez27
Prez27

Reputation: 11

Perl script to join multiple lines into single line after a delimiter

I am getting the messages from the remote system , the response are multi line and i have to convert it into single line based on the delimiter.

the contents of stream reader are like below

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
:

Multiple lines of the response should be converted into single until the delimiter ":"

Please note that im reading it from the remote system, not from a file,its continuous and doesnt have an eof.until the connection with the remote system terminates

Output should be :

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:

Can someone please help with an approach or command to achieve this?

Upvotes: 1

Views: 7729

Answers (2)

Vijay
Vijay

Reputation: 67319

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

tested below:

> 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 :
>

Upvotes: -1

Guru
Guru

Reputation: 17054

One way:

$ 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:

Upvotes: 4

Related Questions