Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


LinkBack Search this Thread
Old 02-11-2009, 05:28 PM   #1
LQ Newbie
Registered: Dec 2008
Posts: 11

Rep: Reputation: 0
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline?

Ok I have been working on tons of other little problems lately and this one is probably simple to solve. I just can't get my head around it.

I have many one line files and I cat them all to a new file - I keep the new line at the end of each line.

For example if I use a simple loop:

for i `ls -1`
cat $i >> newfile.txt

where i = a bunch of files that are one line long - I end up with a one line file.

if I do this:

cat file1 file2 file3 file4 file5 file6 file7 file8 >> newfile.txt

I get a file with 8 lines.


How best can I take an array of file names and pipe the files into a new file?


Last edited by tmcguinness; 02-11-2009 at 05:33 PM. Reason: didn't finish
Old 02-11-2009, 07:03 PM   #2
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
I'm not sure what could be different, but if I have three one line files (file1, file2, file3 with contents of 'one', 'two' and 'three' respectively) and I do what you describe first, I get a three-line file:
telemachus $ for item in file*
> do
> cat $item >> composite
> done
telemachus $ cat composite
Old 02-11-2009, 07:13 PM   #3
Registered: Oct 2008
Location: Brisbane Australia
Distribution: always back to Fedora
Posts: 241

Rep: Reputation: 50
This works for me :-

for i in `ls -1 | grep -v newfile.txt`
cat $i >> newfile.txt

(I'm assuming you had a typo in your for loop)
Old 02-11-2009, 09:57 PM   #4
LQ Newbie
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Ok so generally I don't put things in the same location as they start in, but if I do I am path specific about everything...

And as I test things now - neither way works - there is no newline in the single line file?

Anyone want to post a quick newline creation sed or awk oneliner?
Old 02-12-2009, 06:38 AM   #5
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Compare the output of these - does the second have double newlines?
cat filename

perl -pe '$_ .= "\n"' filename
That Perl one-liner adds a newline to the end of every line.


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question for i in `cat move.txt` Noto Programming 13 05-25-2008 06:32 PM
how to md5 a txt graziano1968 Linux - General 4 02-21-2007 04:36 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
robots.txt paleogryph Linux - Software 1 11-11-2005 02:32 PM
txt >> mysql LLS Programming 2 06-21-2005 11:15 AM

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

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