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

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

How to join two text lines on one l

Reply to Message Icon

Original Message
Name: lamos
Date: August 18, 2005 at 18:34:01 Pacific
Subject: How to join two text lines on one l
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.


Report Offensive Message For Removal


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


Report Offensive Follow Up For Removal

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!



Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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.


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal







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



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

How to conver a text file into .xls
    Summary: Hi, How to convert a text file (which is tab delimited) into an .xls file. in unix scripting. I tired renaming the extension of .txt into .xls. It seems to work so but the leading zeros of the cell co...
www.computing.net/answers/unix/how-to-conver-a-text-file-into-xls/7761.html

To combine two files to one file
    Summary: Hi , I wish to know how to combine two files to one file using unix command. I had two files aaa and bbb aaa files: 02 02 02 02 02 02 and bbb files : 5646 5646 5646 5646 5646 5646 I need to put it to...
www.computing.net/answers/unix/to-combine-two-files-to-one-file/7204.html

how do you get pthread to wait for
    Summary: I am trying to figure out how to have two pthreads interact with one another...I am using mutex to lock the threads out but, the other threads don't stop where they are. How do you get the other thre...
www.computing.net/answers/unix/how-do-you-get-pthread-to-wait-for-/6499.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History






Data Recovery Software