Computing.Net > Forums > Unix > cat concatenate deleting first line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

cat concatenate deleting first line

Reply to Message Icon

Name: kotani
Date: November 18, 2008 at 22:21:30 Pacific
OS: OS X 10.5
CPU/Ram: Intel
Manufacturer/Model: -
Comment:

Hello. I am trying to concatenate multiple csv files using
cat, and it works. It's just that in every file besides the
first one, the first line gets deleted.

This is what I'm using.

cat /combine/*.csv > /combined.csv

Does anyone know if there is a way for cat to keep all the
files intact? Thanks!


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 18, 2008 at 23:14:42 Pacific
Reply:

I'm not understanding why you are having this problem. It's a simple enough use of cat:

cat /combine/*.csv > /combined.csv

One thing I'd like to warn you about is the above command will not work unless you have root privileges. You are attempting to create combined.csv file in the root directory:

/combined.csv

I don't think this is a good idea even if you have the file creation rights.


Report Offensive Follow Up For Removal

Response Number 2
Name: kotani
Date: November 19, 2008 at 17:41:25 Pacific
Reply:

Thank you for your response. I took out the path to the
actual directory and the resulting file path, so I'm not actually
creating a file on root.

I think I found out why... I am not getting a end of line after
each file, squeezing the last and first entry between each file.
Is there a way for cat to insert a "end of line" character after
each file? Again, Thanks for your help!


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: November 19, 2008 at 21:20:27 Pacific
Reply:

You're welcome. How silly of me! I've seen this issue before.

One fix is to add a newline to the end of the offending file. Do something like this:

echo "" >> myfile.csv

I don't think there's anything that cat alone can do, but this script lists out each file individually, appends the file to the combined.csv file, and appends a newline to the combined.csv file:


#!/bin/ksh

ls -1 *.csv|while read file
do
echo "$file" >> combined.csv
echo "" >> combined.csv
done



Report Offensive Follow Up For Removal
Reply to Message Icon

Related Posts

See More







Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: cat concatenate deleting first line

Delete first line of a file www.computing.net/answers/unix/delete-first-line-of-a-file/7594.html

Delete all lines containing pattern www.computing.net/answers/unix/delete-all-lines-containing-pattern/4499.html

Get the first line from a file www.computing.net/answers/unix/get-the-first-line-from-a-file/7981.html