Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line sorcerery

I have a directory full of .xls files that I want to convert to .csv. I'm using xls2csv. This command only prints out the csv to the screen so I believe you have to do xls2csv (xls file) > (new file).csv. So for this I need to write a loop.

for f in `ls`; do xls2csv > `rev $f` | cut -d "." | rev | echo ".csv"

That's what I have so far and it doesn't work. I'm just hoping you can understand exactly what I want to do by the above example.

like image 999
Ste Avatar asked Jul 03 '26 08:07

Ste


2 Answers

for f in *.xls; do
  basename="${f%.xls}"
  csvname="$basename.csv"
  xls2csv "$f" > "$csvname"
done

[update] fixed the typo, so that $basename is actually used. Thanks.

like image 101
Roland Illig Avatar answered Jul 05 '26 02:07

Roland Illig


GNU Parallel has a feature for this: {.} which is the original string but with the .extension removed:

ls | parallel xls2csv {} ">" {.}.csv

Plus you get the added bonus that xls2csv will be run in parallel if you have multiple CPUs. It also deals correctly with file names like:

My Brother's 12" records.xls

To learn more watch the intro video: http://www.youtube.com/watch?v=OpaiGYxkSuQ

like image 31
Ole Tange Avatar answered Jul 05 '26 04:07

Ole Tange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!