Awk Commands to replace filenames

nabbisen - May 11 '21 - - Dev Community

(Gnu's) Awk is a scripting language to manipulate text data.
This post shows how I replaced filenames using awk Commands.

I had translation dictionary files in Swedish aka "Sv"enska.

$ ls -1 translations/*.sv.*
translations/about.sv.xliff
...
translations/validators.sv.xliff
Enter fullscreen mode Exit fullscreen mode

Well, my goal was to get cp commands to duplicate them such as:

$ cp translations/about.sv.xliff translations/about.ja.xliff
Enter fullscreen mode Exit fullscreen mode

I was not used to awk.
Therefore, I first tried to print the same list with awk's print.

$ ls -1 translations/*.sv.* | \
    awk '{ print $1; }'
translations/about.sv.xliff
...
translations/validators.sv.xliff
Enter fullscreen mode Exit fullscreen mode

Got ๐Ÿ™‚
Next, I found awk has gsub as a standard library function. "gsub" exists in Ruby and I was familiar with it.

$ ls -1 translations/*.sv.* | \
    awk '{ gsub(".sv.", ".ja.", $1); print $1; }'
translations/about.ja.xliff
...
translations/validators.ja.xliff
Enter fullscreen mode Exit fullscreen mode

I succeeded in replacing languages in filenames.
Then I lined up the original and the replaced.

$ ls -1 translations/*.sv.* | \
    awk '{ print $1; gsub(".sv.", ".ja.", $1); print $1; }'
translations/about.sv.xliff
translations/about.ja.xliff
...
...
translations/validators.sv.xliff
translations/validators.ja.xliff
Enter fullscreen mode Exit fullscreen mode

Got, but I didn't want new lines between the original and the replaced.
Thus, I converted print to printf.

$ ls -1 translations/*.sv.* | \
    awk '{ printf $1; gsub(".sv.", ".ja.", $1); print $1 }'
translations/about.sv.xlifftranslations/about.ja.xliff
...
translations/validators.sv.xlifftranslations/validators.ja.xliff
Enter fullscreen mode Exit fullscreen mode

It seemed necessary to insert space. Anyway, it was coming there soon.

ls -1 translations/*.sv.* | \
    awk '{ printf "cp "$1" "; gsub(".sv.", ".ja.", $1); print $1";" }'
cp translations/about.sv.xliff translations/about.ja.xliff;
...
cp translations/validators.sv.xliff translations/validators.ja.xliff;
Enter fullscreen mode Exit fullscreen mode

I got the commands above. I ran them. They were successful.
After all, I got thanks to awk commands:

$ ls -1 translations/*.ja.*
translations/about.ja.xliff
translations/daterangepicker.ja.xliff
translations/exceptions.ja.xliff
translations/flashmessages.ja.xliff
translations/invoice-calculator.ja.xliff
translations/invoice-numbergenerator.ja.xliff
translations/invoice-renderer.ja.xliff
translations/messages.ja.xliff
translations/pagerfanta.ja.xliff
translations/plugins.ja.xliff
translations/system-configuration.ja.xliff
translations/validators.ja.xliff
Enter fullscreen mode Exit fullscreen mode

So sweet ๐Ÿ™‚

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .