Mel
Mel

Reputation: 11

Joining lines in a text file

Need some help with developing a script to join lines in a text file, ie

Blah
\Blah Blah\Bhal
1

Blah2
\Blah Blah\Blah2
600

to:

Blah:\Blah Blah\Bhal:1
Blah2:\Blah Blah\Blah2:2

Output was generated via a DOS batch file, but I can also do the parsing in BASH if it will be any easier.

Upvotes: 1

Views: 2222

Answers (2)

egorulz
egorulz

Reputation: 1505

The most elegant option in my opinion is to use paste

To join 2 lines

paste -sd'::\n'

To join 3 lines

paste -sd':::\n'

and so on.

Upvotes: 4

Federico Builes
Federico Builes

Reputation: 5107

You can use tr to change the newlines (\n) for something else:

tr "\\n" ":" < myfile

This would replace every new line for a :.

Keep in mind that if you have:

foo

bar

The result will be foo::bar (since you have two newlines). Change accordingly.

Upvotes: 2

Related Questions