LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2009, 05:50 PM   #1
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Rep: Reputation: 0
Need to combine text on to one line


I know this should be easier than I am making it out to be. I need to combine lines of 8 lines of text to one line. All of the examples I have been able to find are not working for me so I must be doing something wrong.

text file:

one
two
three
four
.
.
.
eight

one
two
three
.
.
eight

Requested output:

one two three....eight
one two three ... eight

I have tried every example I can find but when I look at the output file it looks just like the imput file. Could somebody please suggest a sed command that I can try for this example.

Chris
 
Old 05-11-2009, 05:55 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
~/tmp$ cat eights                                                                                                                
one
two
three
four
five
six
seven
eight

one2
two2
three2
four2
five2
six2
seven2
eight2

one3
two3
three3
four3
five3
six3
seven3
eight3
~/tmp$ awk 'BEGIN{RS="";ORS="\n";FS="\n";OFS=" "}{$1=$1;print $0}' eights                                                        
one two three four five six seven eight
one2 two2 three2 four2 five2 six2 seven2 eight2
one3 two3 three3 four3 five3 six3 seven3 eight3

Cheers,
Tink
 
Old 05-11-2009, 07:53 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Using GNU sed
Code:
sed '/^$/d;N;N;N;N;N;N;N;s/\n/ /g' infile > outfile
 
Old 05-11-2009, 10:08 PM   #4
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Tinkster/Kenhelm

I thank you both for your replys.

I am sorry to say I am too ignorant to figure out how to apply Tinksters suggestion. I do not understand ~tmp$ I tried replacing it with my input file however I guess that wasn't the answer.

Kenhelm's sed command seemed to work but when I opened up the output file with a text editor all I had managed to do was put a space at the beginning of each line. When viewed using the view command the file appeared ordered properly however there was a ^M in between each text entry instead of a space.

I don't know.

Chris
 
Old 05-11-2009, 10:44 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
That means that your file comes from a DOS/Windows box.

Convert it before applying either command (sed or awk).

See whether fromdos or dos2unix exist on your machine.


Cheers,
Tink
 
Old 05-11-2009, 10:47 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by clstanton View Post
T
I do not understand ~tmp$ I tried replacing it with my input file however I guess that wasn't the answer.
There's nothing to understand/enter there :}

It's just the tmp directory in my users home.
Code:
awk 'BEGIN{RS="";ORS="\n";FS="\n";OFS=" "}{$1=$1;print $0}' file

Cheers,
Tink
 
Old 05-11-2009, 10:50 PM   #7
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Thanks again Tink. I'll give it another try tomorrow. Again I appreciate the help.

Chris
 
Old 05-12-2009, 01:21 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
^M is the carriage return character, also known as \r.
Windows uses \r\n at the end of each line.
Linux uses \n.
\r can be put in GNU sed expressions.
To convert a Windows text file for Linux the \r's can be removed with sed 's/\r$//'
Code:
sed 's/\r$//' dosfile | sed '/^$/d;N;N;N;N;N;N;N;s/\n/ /g' > linuxfile
To convert the file back to the Windows format add \r to the end of each line with
Code:
sed 's/$/\r/' linuxfile > dosfile
 
Old 05-12-2009, 01:44 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python, here's something more understandable.
Code:
for line in open("file"):    
    if line=="\n":
        print 
    else:
        print line.strip(),  #print side by side.
output:
Code:
# python test.py
one two three four five six seven eight
one2 two2 three2 four2 five2 six2 seven2 eight2
one3 two3 three3 four3 five3 six3 seven3 eight3
 
Old 05-12-2009, 03:28 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Using shell:
Code:
$ cat file | xargs -n8 echo
one two three four five six seven eight
one2 two2 three2 four2 five2 six2 seven2 eight2
one3 two3 three3 four3 five3 six3 seven3 eight3
To convert files written in dos/windows to unix format, you can try
Code:
dos2unix file
 
Old 05-12-2009, 07:55 AM   #11
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks eveyone I've got it now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
Combine text to forms image carstenson Linux - Software 0 12-15-2004 05:56 PM
Combine number and text Ephracis Programming 3 11-17-2004 12:13 PM
I need a program to combine text with gif files stephenoregan4 Linux - Software 1 02-16-2004 10:29 AM


All times are GMT -5. The time now is 04:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration