O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Renaming files in a directory with AWK

We can use AWK to create and execute shell commands, such as the mv command for renaming, by piping them to shell sh for executing them. We should always check a command by printing the output before piping it to sh to avoid any typo errors, as follows :

First, we will just use the print statement to display the command result and not execute it on the command prompt, as follows :

$ ls | awk '{ printf("mv \"%s\" \"%s\"\n", $0, toupper($0)) }'

If we confirm the command line built using AWK, then we can execute it by piping the AWK output to the sh command, as follows:

$ ls | awk '{ printf("mv \"%s\" \"%s\"\n", $0, toupper($0)) }' | sh

In out next rename examples, we substitute the occurrence of spaces ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required