I've a .po file I need to copy msgid value into msgstr value if msgstr is empty.
For example
msgid "Hello"
msgstr ""
msgid "Dog"
msgstr "Cane"
Should become
msgid "Hello"
msgstr "Hello"
msgid "Dog"
msgstr "Cane"
Currently, for testing purpose, I'm working with another file, but final script will works inline.
#!/bin/bash
rm it2.po
sed $'s/^msgid.*/&\\\n---&/' it.po > it2.po
sed -i '/^msgstr/d' it2.po
sed -i 's/^---msgid/msgstr/' it2.po
This script has 2 problems (at least):
Any help would be appreciated. Thanks in advance.
You may consider better tool gnu awk
instead of sed
:
awk -i inplace -v FPAT='"[^"]*"|\\S+' '$id != "" && $1 == "msgstr" && (NF==1 || $2 == "\"\"") {$2=id} $1 == "msgid" {id=$2} 1' file
msgid "Hello"
msgstr "Hello"
msgid "Dog"
msgstr "Cane"
-v FPAT='"[^"]*"|\\S+'
makes a quoted string or any non-whitespace field an individual field.
A more readable form:
awk -i inplace -v FPAT='"[^"]*"|\\S+' '
$id != "" && $1 == "msgstr" && (NF==1 || $2 == "\"\"") {$2=id}
$1 == "msgid" {id=$2}
1' file