Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining lines from a text file in Unix?

Tags:

shell

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.

like image 613
user1738853 Avatar asked Oct 25 '12 14:10

user1738853


1 Answers

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'
like image 153
dogbane Avatar answered Sep 19 '22 15:09

dogbane