Awk: how to convert PNG files into PDF

Here is a method to convert a bunch of PNG files into another format (here PDF). This method works under linux and uses both awk and convert commands (the conversion is made with the convert command).

The following script generates the commands that are required to perform this task. Here, we redirect the output into a shell file ( that can be executed later on.

ls *.png | awk '{print "convert  " $1 "  " substr($1, 1,length($1)-4)".pdf"}'  >
chmod 755

Some explanations:

ls *.png |

sends a list of PNG files to awk (thanks to the pipe sign).

substr($1, 1, length($1)-4)

The substr() function (from AWK) replaces the .png extension into .pdf

The convert command does the real work, that is the conversion and accepts many optional arguments that you can play with to be more specific about the output format (size, resolution, …).

Please follow and like us:
This entry was posted in Linux, Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.