LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-11-2009, 05:28 PM   #1
tmcguinness
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`
do
cat $i >> newfile.txt
done

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
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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:
Code:
telemachus $ for item in file*
> do
> cat $item >> composite
> done
Output:
Code:
telemachus $ cat composite
one
two
three
 
Old 02-11-2009, 07:13 PM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
This works for me :-

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

(I'm assuming you had a typo in your for loop)
 
Old 02-11-2009, 09:57 PM   #4
tmcguinness
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
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

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

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


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
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

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

All times are GMT -5. The time now is 07:25 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