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 |
Code:
~/tmp$ cat eights Cheers, Tink |
Using GNU sed
Code:
sed '/^$/d;N;N;N;N;N;N;N;s/\n/ /g' infile > outfile |
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 |
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 |
Quote:
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 |
Thanks again Tink. I'll give it another try tomorrow. Again I appreciate the help.
Chris |
^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 Code:
sed 's/$/\r/' linuxfile > dosfile |
if you have Python, here's something more understandable.
Code:
for line in open("file"): Code:
# python test.py |
Using shell:
Code:
$ cat file | xargs -n8 echo Code:
dos2unix file |
Thanks eveyone I've got it now.
|
All times are GMT -5. The time now is 10:53 PM. |