[英]Replace an occurrence in several files with sed
I have more than 30 files where I have to replace the occurrence我有 30 多个文件,我必须替换出现的文件
msgstr ""
preceded by msgid "(aged %s)
msgstr ""
前面有msgid "(aged %s)
by the contents of通过的内容
msgstr "..."
belonging to the first occurrence of msgid "(aged %s)
msgstr "..."
属于第一次出现的msgid "(aged %s)
All the files are located in directory resources/
:所有文件都位于目录resources/
中:
resources
lang-fr
file.po
lang-ge
file.po
lang-it
file.po
If this could help there are only 3 occurrences of msgid "(aged %s)
per file.如果这有帮助的话,每个文件只有 3 次出现msgid "(aged %s)
。
File: file.po
文件: file.po
...
...
msgid "(aged %s)
msgstr "good translation %s"
...
...
msgid "(aged %s)
msgstr ""
...
....
msgid "(aged %s)
msgstr ""
....
....
Expected result:预期结果:
File: file.po
文件: file.po
....
...
msgid "(aged %s)
msgstr "good translation %s"
...
...
msgid "(aged %s)
msgstr "good translation %s"
...
....
msgid "(aged %s)
msgstr "good translation %s"
....
....
I've spent a few hours trying with sed, but I'm stuck.我花了几个小时尝试使用 sed,但我卡住了。
With a shell variable, and two instances of sed
:使用 shell 变量和sed
的两个实例:
r="$(sed -n '/^msgid "(aged %s)$/{n;s/^[^"]*"//p;q}' file.po)"
sed '/^msgid "(aged %s)$/{n;s/\(^msgstr "\)"$/\1'"$r"'/}' file.po
After the first run of sed
, the variable $r
will contain good translation %s"
. The next sed
pass looks for ""
and substitutes it with $r
.在第一次运行sed
之后,变量$r
将包含正确good translation %s"
。接下来的sed
遍查找""
并将其替换为$r
。
It could also be done uglier without the variable:如果没有变量,它也可以做得更丑:
sed '/^msgid "(aged %s)$/{n;s/\(^msgstr "\)"$/\1'"$(sed -n '/^msgid "(aged %s)$/{n;s/^[^"]*"//p;q}' file.po)"'/}' file.po
See also Baeldung 's useful survey Using sed to Replace a Multi-Line String .另请参阅Baeldung的有用调查使用 sed 替换多行字符串。
For now I have only following script which do not what expected现在我只有下面的脚本,这不是预期的
#!/bin/bash
find resources/lang -name '*.po' -exec sed '/^msgid "(aged %s)$/!b
n;/^msgstr "/!b
x;/^$/x;h' {} \;
For now unfortunately Input = Output现在不幸的是输入= Output
#: app/Age.php:183
#, php-format
msgid "(aged %s)"
msgstr "objective"
#. I18N: The age of an individual at a given date for a Male individual
#: app/Age.php:184
#, php-format
msgctxt "MALE"
msgid "(aged %s)"
msgstr ""
#. I18N: The age of an individual at a given date for a Female individual
#: app/Age.php:188
#, php-format
msgctxt "FEMALE"
msgid "(aged %s)"
msgstr ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.