Issue
I am trying to generate a .po file out of an existing one, needing the basic en_US
resource file.
It contains sequences of this kind:
# DE language source file.
#
"Project-Id-Version: ID-VERSION\n"
"Report-Msgid-Bugs-To: <[email protected]>\n"
"POT-Creation-Date: 2023-01-01 00:01+0200\n"
"PO-Revision-Date: 2023-01-02 00:02+0200\n"
"Last-Translator: somebody\n"
"Language-Team: somebody\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ISO-8859-15\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "(none)"
msgstr "(keiner)"
msgid "Unknown error"
msgstr "Unbekannter Fehler"
msgid "The lazy fox jumps over brown dog"
msgstr "Der faule Fuchs springt über braunen Hund"
that I try to get like:
# DE language source file.
#
"Project-Id-Version: ID-VERSION\n"
"Report-Msgid-Bugs-To: <[email protected]>\n"
"POT-Creation-Date: 2023-01-01 00:01+0200\n"
"PO-Revision-Date: 2023-01-02 00:02+0200\n"
"Last-Translator: somebody\n"
"Language-Team: somebody\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ISO-8859-15\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "(none)"
msgstr "(none)"
msgid "Unknown error"
msgstr "Unknown error"
msgid "The lazy fox jumps over brown dog"
msgstr "The lazy fox jumps over brown dog"
I'd prefer sed
or awk
to do the job, but I am not aware how to capture the (none)
part and copy it to the next line instead of (keiner)
.
I think of a replacement, but also capture to add directly a split line with \n
to generate a second line (for msgstr
), and delete the pair line (the one with msgstr
).
How could this be achieved with sed
or awk
?
Solution
You could use:
$ awk '$1 == "msgid" {t=$1; $1=""; v=$0; $1=t} $1 == "msgstr" {$0=$1 v}; 1' file
msgid "(none)"
msgstr "(none)"
msgid "Unknown error"
msgstr "Unknown error"
msgid "The lazy fox jumps over brown dog"
msgstr "The lazy fox jumps over brown dog"
whenever msgid is found, store all but the first column in a variable and then whenever msgstr is found, replace all but the first column with the variable.
Answered By - Paolo Answer Checked By - Clifford M. (WPSolving Volunteer)