Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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/*.csvWhat 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.csvThank you for your help.

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

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

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.

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.

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.

It works now after i did the chmod. Thanks! :)
instead of
mv *done DONEDONE 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.

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.

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!

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |