LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2005, 04:43 PM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
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
 
Old 01-19-2005, 04:50 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

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

'cat file1 file2 > file1' will do the trick about copying.
 
Old 01-19-2005, 06:39 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 01-19-2005, 07:31 PM   #4
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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
 
Old 01-19-2005, 10:06 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 01-20-2005, 09:51 AM   #6
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
Old 01-20-2005, 10:02 AM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

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

$ echo `cat nn | tr '\n' ' '`
 
Old 01-20-2005, 11:16 AM   #8
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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!
 
  


Reply



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
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
GUI never loads dbernat32 Mandriva 1 03-31-2005 07:22 PM
Everything Loads Slowly mandymo Mandriva 5 12-12-2004 11:36 AM
installed dropline, root loads kde3.2, user loads drop pgrimes Linux - Software 7 06-28-2004 06:11 PM
TV out - Works up until X loads bkeating Linux - Hardware 3 11-21-2002 02:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 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
Open Source Consulting | Domain Registration