LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Flatten something with loads of lines (https://www.linuxquestions.org/questions/programming-9/flatten-something-with-loads-of-lines-279864/)

genderbender 01-19-2005 04:43 PM

Flatten something with loads of lines
 
This might sound a dumb question and i imagine the answers very basic but how would I turn a file with lots of lines into one file with one line with the same contents.

So I've got a file like this:

CUSTOMER
name John
surname Smith
telephone number 379471
age 22

and I would like it like this:

name John surname Smith telephone number 379471 age 22

I need to do it in bash

On a seperate note is there any way of adding text from one file to another? I've got another customer file id like to add to the end of this one.

Thanks a lot, help is much appreciated :)

jonaskoelker 01-19-2005 04:50 PM

python -c "print (''.join(file('filename').readlines())).replace('\n', ' ')" does the first thing

'cat file1 file2 > file1' will do the trick about copying.

homey 01-19-2005 06:39 PM

Quote:

On a seperate note is there any way of adding text from one file to another? I've got another customer file id like to add to the end of this one.
To add text to the bottom of a file you would use >> instead of > because the second method overwrites the entire file with the entire contents of the first one.
cat file.txt >> file1.txt

genderbender 01-19-2005 07:31 PM

Thanks for all the help, I really appreciate it! I should of got the cat thing, spose i just didnt research enough. I'm still having problems putting all values onto one line because i cant use python. Its bash only :(

I'm thinking sed would do it, I just havent worked out how to do it. I can only use bash though :(


Thanks everyone

homey 01-19-2005 10:06 PM

Quote:

I can only use bash though
Here's one way to get rid of the newline ( \n ) and output to another file. Don't forget the difference between > and >>
Code:

cat file.txt| tr '\n' ' ' > file1.txt

dustu76 01-20-2005 09:51 AM

Quote:

cat file.txt| tr '\n' ' ' > file1.txt
This also kills the last newline (\n) which is not what you'd want normally.

Code:

[/home/soumen/tmp] $ cat nn
abcd
hello
world
[/home/soumen/tmp] $ cat nn | tr '\n' ' '
abcd hello world [/home/soumen/tmp] $

Notice the prompt on the same line. What you'd probably want is:

Code:

[/home/soumen/tmp] $ my_command
abcd hello world
[/home/soumen/tmp] $

Just my 2p.

jonaskoelker 01-20-2005 10:02 AM

[also kills the last newline] ... in which case you want this:

$ echo `cat nn | tr '\n' ' '`

genderbender 01-20-2005 11:16 AM

Actually the post was spot on, it did exactly what I wanted and my nasty bit of skoolwork is slowly developing into a fully functional bit of complex code.

Thanks for your help everyone and for super quick responses!


All times are GMT -5. The time now is 08:03 PM.