2013-10-02 50 views
0

我有一個文件的結構如下:互換兩行腳本

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,在括號之間,你能給我提示寫腳本還是通過命令行來完成?

+0

你爲什麼不只是交換與''msgstr'或msgid'你有沒有相對保持自己的位置? –

+0

是這樣爲前: MSGID「找不到網頁」 msgstr「頁面非trouvée」 成爲: MSGID「頁面非trouvée」 msgstr「找不到網頁」 – molwiko

+0

這是同樣的事情! –

回答

2

您可以使用awksed來執行此操作。以下作品。

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交換,與該正則表達式msg.*和管它sed匹配線。首先sedmsgid重命名爲msgidt,然後重命名爲msgstrmsgid,最後將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" 
+0

謝謝我需要使用iso-8859-1 保存文件上的輸出我嘗試了>文件,但它保存在UTF-8上? – molwiko

+0

你可以使用'iconv -f UTF-8 -t ISO-8859-1 in.txt> out.txt' – iamauser