LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to combine text on to one line (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-combine-text-on-to-one-line-725287/)

clstanton 05-11-2009 04:50 PM

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

Tinkster 05-11-2009 04:55 PM

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

Kenhelm 05-11-2009 06:53 PM

Using GNU sed
Code:

sed '/^$/d;N;N;N;N;N;N;N;s/\n/ /g' infile > outfile

clstanton 05-11-2009 09:08 PM

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

Tinkster 05-11-2009 09:44 PM

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

Tinkster 05-11-2009 09:47 PM

Quote:

Originally Posted by clstanton (Post 3537715)
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

clstanton 05-11-2009 09:50 PM

Thanks again Tink. I'll give it another try tomorrow. Again I appreciate the help.

Chris

Kenhelm 05-12-2009 12:21 AM

^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

ghostdog74 05-12-2009 12:44 AM

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


colucix 05-12-2009 02:28 AM

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

clstanton 05-12-2009 06:55 AM

Thanks eveyone I've got it now.


All times are GMT -5. The time now is 10:53 PM.