LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? (http://www.linuxquestions.org/questions/programming-9/cat-onelinefile-txt-newfile-txt%3B-cat-twofile-txt-newfile-txt-keep-newline-703998/)

tmcguinness 02-11-2009 05:28 PM

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?


--

Telemachos 02-11-2009 07:03 PM

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


ArfaSmif 02-11-2009 07:13 PM

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)

tmcguinness 02-11-2009 09:57 PM

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?

Telemachos 02-12-2009 06:38 AM

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.


All times are GMT -5. The time now is 11:45 AM.