user1738853
user1738853

Reputation: 85

Combining lines from a text file in Unix?

I have a file that outputs lines of the following form:

$XYZ blah blah blah
$XYZ something
$XYZ random data

The "$XYZ" prefix is the same for every line, it really does start with a dollar sign, and it's highly unlikely to occur anywhere but the start of a line.

The only way I can get at this file is via screen capture, which causes the lines to wrap at 80 characters. So it looks like the following (if you pretend wrapping is at a smaller number than 80):

$XYZ blah bl
ah blah
$XYZ somethi
ng
$XYZ random 
data

I'd like to recreate the real lines from that. I could write a program to do it, but I'm thinking there might be some Unix command that I'm not familiar with that might make it easy. Any ideas?

Thanks in advance.

Upvotes: 5

Views: 487

Answers (3)

glenn jackman
glenn jackman

Reputation: 247012

The paste command is for exactly this purpose:

paste -d "" - -

As in

echo '$XYZ blah bl
ah blah
$XYZ somethi
ng
$XYZ random 
data' | paste -d "" - -

outputs

$XYZ blah blah blah
$XYZ something
$XYZ random data

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185530

In perl :

perl -pe 's/\n//g;s/XYZ\s+/\n$&/g' filename.txt

Upvotes: 1

dogbane
dogbane

Reputation: 274758

First, merge all the lines into one long line and then add a newline wherever you see the word which is supposed to be the start of a line, like this:

tr -d '\n' < file | sed 's/XYZ/\nXYZ/g'

Upvotes: 4

Related Questions