(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
Well, my goal was to get cp
commands to duplicate them such as:
$ cp translations/about.sv.xliff translations/about.ja.xliff
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
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
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
Got, but I didn't want new lines between the original and the replaced.
Thus, I converted print
to print
f
.
$ 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
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;
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
So sweet ๐