Generating Commands with Awk and Sed


From Docunext Technology Wiki

Jump to: navigation, search

I often find it helpful to generate commands using sed and awk.

For instance, I wanted to rename a bunch of files named invoice3288734.pdf to 3288734_invoice.pdf.

ls -1 | awk '{print "mv "$1, $1}' | sed -r "s/pdf\ [^0-9]+([0-9]+)[^0-9]+/pdf\ \1_invoice.pdf/g"

I list all the files, print the names twice with a preceding "mv" command, edit the second filename to suit my needs. I can even redirect the output to a new file, and then execute that file.

This methodology probably has some downfalls I haven't encountered yet, but it sometimes makes complicated work easier for me.

Personal tools