Bash 交换两行脚本

Bash 交换两行脚本,bash,shell,Bash,Shell,我的文件结构如下: msgid "" msgstr "" "PO-Revision-Date: 2013-04-08 10:12:14+0000\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=n > 1;\n"

我的文件结构如下:

 msgid ""
    msgstr ""
    "PO-Revision-Date: 2013-04-08 10:12:14+0000\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=2; plural=n > 1;\n"
    "X-Generator: GlotPress/0.1\n"
    "Project-Id-Version: Modern Theme\n"

    #: 404.php:34
    msgid "Page not found"
    msgstr "Page non trouvée"

    #: alert-form.php:6
    msgid "You have sucessfully subscribed to the alert"
    msgstr "Vous vous êtes inscrit avec succès à cette alerte"

    #: alert-form.php:7 user-change_email.php:42
    msgid "Invalid email address"
    msgstr "Adresse e-mail incorrecte"

    #: alert-form.php:8
    msgid "There was a problem with the alert"
    msgstr "Il y a un problème avec cette alerte"

    #: alert-form.php:39
    msgid "Subscribe to this search"
    msgstr "Abonnez vous à cette recherche"

    #: alert-form.php:55
    msgid "Subscribe now"
    msgstr "Abonnez-vous maintenant"

    #: contact.php:35
    msgid "Contact us"
    msgstr "Contactez-nous"

我想交换包含字符串msgid的行的内容,在包含内容的括号之间,在包含msgstr的连续行上,在括号之间,您可以给我提示编写脚本或通过命令行执行吗?

您可以使用
awk
sed
来执行此操作。以下作品

awk '/msg.*/{getline x;print x;}1' file | sed -e 's/msgid/msgidt/g' -e 's/msgstr/msgid/g' -e 's/msgidt/msgstr/g'
awk
交换与regex
msg.*
匹配的行,并将其输送到
sed
。首先
sed
msgid
重命名为临时
msgidt
,然后将
msgstr
重命名为
msgid
,最后将
msgidt
重命名为
msgid

结果:

msgid ""
msgstr ""
"PO-Revision-Date: 2013-04-08 10:12:14+0000\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n > 1;\n"
"X-Generator: GlotPress/0.1\n"
"Project-Id-Version: Modern Theme\n"

#: 404.php:34
msgid "Page non trouvee"
msgstr "Page not found"

#: alert-form.php:6
msgid "Vous vous Cetes inscrit avec succsess  cette alerte"
msgstr "You have sucessfully subscribed to the alert"

#: alert-form.php:7 user-change_email.php:42
msgid "Adresse e-mail incorrecte"
msgstr "Invalid email address"

#: alert-form.php:8
msgid "Il y a un problC(me avec cette alerte"
msgstr "There was a problem with the alert"

#: alert-form.php:39
msgid "Abonnez vous C  cette recherche"
msgstr "Subscribe to this search"

#: alert-form.php:55
msgid "Abonnez-vous maintenant"
msgstr "Subscribe now"

#: contact.php:35
msgid "Contactez-nous"
msgstr "Contact us"

你为什么不直接用
msgstr
交换
msgid
,或者你必须相对保留它们的位置?是的,因此对于ex:msgid“未找到页面”msgstr“未找到页面”变成:msgid“未找到页面”msgstr“未找到页面”是一样的!谢谢,我需要用iso-8859-1将输出保存在文件中,我尝试了>文件,但它保存在UTF-8上?您可以使用
iconv-f UTF-8-t iso-8859-1 in.txt>out.txt