Computing.Net > Forums > Unix > prepend all CSV files in directory

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

prepend all CSV files in directory

Reply to Message Icon

Name: gizmoconcept
Date: March 17, 2009 at 00:55:39 Pacific
OS: Windows XP
Subcategory: General
Comment:

Situation:
I have a directory called ABC which contains multiple CSV files. In each CSV files there are multiple records for e.g. a coat hanger, a plait shirt, a cotton pants , etc in each line in each CSV. I want to delete the first character of every record in each CSV and prepend the word blue. i.e remove "a" and prepend with "blue". So it will will read blue coat hanger, blue plait shirt, blue cotton pants and so on.

What I have so far is:
#!/bin/bash
cat *.csv |sed 's/.\(.*\)/\1/' > *.csv
sed 's/^/blue &/' *.csv > /Done/*.csv

What I want to do:
To run the above script with loop in the ABC directory, which processes all the CSV files contained there and output the prepended CSV files into a directory called DONE.

Need help on:
to amend my script above with the loop function, read all CSV files from ABC directory and output the processed files to DONE directory and rename the files maintaining their original file names but with _done appended to them i.e. filename_done.csv

Thank you for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 17, 2009 at 08:30:56 Pacific
Reply:

How about a loop that works on one file at a time:

#!/bin/bash

# untested
cd ABC
for i in *.csv
do
   sed -e 's/.\(.*\)/\1/' -e 's/^/blue &/' $i > "${1}_done"
done

# now, move the done files to the DONE directory:
mv *done DONE


0

Response Number 2
Name: gizmoconcept
Date: March 17, 2009 at 20:46:16 Pacific
Reply:

Hi nails, thanks for helping me. However, when I tried running it I get an error:

/bin/bash^M: bad interpreter: No such file or directory

Sorry forgot to mention earlier that I am a newbie, so please go slow with me :)


0

Response Number 3
Name: nails
Date: March 17, 2009 at 22:13:31 Pacific
Reply:

No, problem; first, beware that I edited my original post as I had left an extra, incorrect line.

Second, You don't say what your environment is, but it looks like you've ftp'ed the script from your window's box to linux. The reason I can tell is because there is an extra carriage return, ^M, at the end of the line:

/bin/bash^M: bad interpreter:

That is probably what is causing your problem.

The reason is that the Window's line terminator is the Carriage Return/Line Feed where in Unix/Linux it is just the Line Feed. You probably did the ftp in binary mode which kept the extra CR. If you do the ftp in ascii mode, the CR probably goes away.

There is a dos2unix command on most *nix systems, but with a script so short, you can use vi (or vim) and remove the CR at the end of each line.

Let me know if you have any other questions.


0

Response Number 4
Name: gizmoconcept
Date: March 18, 2009 at 03:29:37 Pacific
Reply:

Thanks Nail, you are very helpful indeed.

I have followed your advice and now I get a different error instead:

Permission denied

I think I am getting closer.Thank you again for your help.


0

Response Number 5
Name: nails
Date: March 18, 2009 at 07:19:52 Pacific
Reply:

Probably you haven't set execute permissions. Until you do your script isn't a script - it's just a file. The chmod command changes permissions on a file. Here is one way:

chmod 755 <your file name>

I recommend you check out the MAN page on chmod. It has numerous options.


0

Related Posts

See More



Response Number 6
Name: gizmoconcept
Date: March 19, 2009 at 00:36:36 Pacific
Reply:

It works now after i did the chmod. Thanks! :)

instead of
mv *done DONE

DONE is the output directory, in this I move all processed csv files into the DONE directory.

Can I put it in the same line instead. Meaning all output-ed or processed files goes directly into the DONE directory. So the pre-processed and processed files still share the same filename but are in different directories for e.g.

sed -e 's/.\(.*\)/\1/' -e 's/^/blue &/' $i > /DONE/"${1}"

When I try that nothing happens and it doesn't work.


0

Response Number 7
Name: nails
Date: March 19, 2009 at 09:17:13 Pacific
Reply:


Sure, the destination of the sed command should work:

..... > /DONE/"${1}"

provided you have permission to write/create files in the DONE directory. I'd check if your user id can actually create files in /DONE or DONE.

Keep in mind that the /DONE directory isn't necessarily the same as DONE. /DONE is a directory off the root directory while DONE is a directory located at the current directory.


0

Response Number 8
Name: gizmoconcept
Date: March 23, 2009 at 04:12:49 Pacific
Reply:

It works now! Thank you so much Nails for your help.
It is good people like you that are willing to help others that keeps this forum a must visit for technical advice. Thanks again! Cheers!


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: prepend all CSV files in directory

Comparing all files in current dir www.computing.net/answers/unix/comparing-all-files-in-current-dir/4102.html

Count # of codes in directory www.computing.net/answers/unix/count-of-codes-in-directory/7074.html

Script to process/search a csv file www.computing.net/answers/unix/script-to-processsearch-a-csv-file/5115.html