Iterate Over Files in Bash
November 10, 2010 Leave a comment
Sometimes you need to whip up a dirty bash script to process through some text. I needed to iterate over my database log files that I wrote about from my last post and then output the result to a new file. I wanted the output files to have a different extension, but I wasn’t sure how to get just the filename from bash, fortunately this post helped.
#!/bin/bash
for logfile in *.log
do
fn=${logfile%.*} #cut extension
echo making $fn.data...
egrep '\#\ collect|\:\=' $logfile > $fn.data
done
Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.
Follow me on Twitter @jprichardson and read my blog on software entrepreneurship.
-JP