Computing.Net > Forums > Unix > How to join two text lines on one l

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to join two text lines on one l

Reply to Message Icon

Name: lamos
Date: August 18, 2005 at 18:34:01 Pacific
OS: Solaris 8
CPU/Ram: 1GB
Comment:

Hi folks,

I am trying to figure out how to join two lines on one line of text. For example, I have a text file with the following text:

TEXT LINE1
0
TEXT LINE2
0
TEXT LINE2
234
TEXT LINE3
236
... and so on.

I want it to look as follows:

TEXT LINE1 0
TEXT LINE2 0
TEXT LINE3 234
TEXT LINE4 236
... and so on.

How would I do it?

Thanks for you help...
A real novice.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 19, 2005 at 07:38:44 Pacific
Reply:

Hi:

Here's a sed solution:

sed '$!N;s/\n/ /' myfile

but I'd use paste:

paste -s -d" \n" myfile


0

Response Number 2
Name: lamos
Date: August 19, 2005 at 10:43:23 Pacific
Reply:

Nails,

The paste option worked nicely. However, I received a garbled message for sed option. I'll use paste instead. Since this worked for one, how can I do multiples? For example:

TEXT LINE1
0
0
TEXT LINE2
0
234
... on so on.

I'd like to look as follows:

TEXT LINE1 0 0
TEXT LINE2 0 234
.. and so on.

Thanks for you help!



0

Response Number 3
Name: vicchai
Date: August 21, 2005 at 17:39:54 Pacific
Reply:

Try this:

#change the num to the lines you want to join
num=3
count=$num
cat textfile | while read lines
do
if [ "$count" != "1" ]; then
echo $lines | tr '\n' ' '
count=`expr $count - 1`
else
echo $lines
count=$num
fi
done


0

Response Number 4
Name: lamos
Date: August 23, 2005 at 12:55:22 Pacific
Reply:

Hi Vicchai,

Thanks for your suggestion. I used Nails paste command suggestion to perform the task. tmp1.dat contained the above text and numbers.

cat tmp1.dat | paste - - - > tmp2.dat

tmp2.dat contains the numbers on the same line as the text line.

Thanks.


0

Response Number 5
Name: Luke Chi
Date: September 8, 2005 at 13:49:07 Pacific
Reply:

To the question:

how can I do multiples? For example:

TEXT LINE1
0
0
TEXT LINE2
0
234
... on so on.

I'd like to look as follows:

TEXT LINE1 0 0
TEXT LINE2 0 234
.. and so on.

Use:

paste -s -d" \n" p

Luke Chi


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to join two text lines on one l

How to conver a text file into .xls www.computing.net/answers/unix/how-to-conver-a-text-file-into-xls/7761.html

To combine two files to one file www.computing.net/answers/unix/to-combine-two-files-to-one-file/7204.html

how to fetch the Nth line from file www.computing.net/answers/unix/how-to-fetch-the-nth-line-from-file/8345.html