2012-11-11 22 views
0

我以sed開頭。 任何人都可以引導我如何解決這個問題? 我開始用這個和我的sed的現在基本命令:在sed中使用字幕

{0}{20}First subtitle 
{30}{50}Second subtitle|New line is made this way. 
{70}{100}Third. 
{1010}{1033}Fourth etc. 

括號中的數字是指開始和結束,其中字幕應該是可見的。 讓我們有一個翻譯誰將使字幕翻譯這樣( 我會簽署這份文本(*)):

{0}{20}First subtitle 
Translation of the first subtitle. 
{30}{50}Second subtitle|New line is made this way. 
Translation of the second subtitle.|Second line of translation of the second subtitle. 
{70}{100}Third. 
Translation of third. 
{1010}{1033}Fourth etc. 
Translation of fourth etc. 

我需要做3件事情: 1)獨立的翻譯字幕:

{0}{20}Translation of the first subtitle. 
{30}{50}Translation of the second subtitle|Second line of translation of the second subtitle. 
{70}{100}Translation of third. 
{1010}{1033}Translation of fourth etc. 

2)從兩個字幕(帶*號)只有原來subtiltes簽署的文本分開,並得到這個:

{0}{20}First subtitle 
{30}{50}Second subtitle|New line is made this way. 
{70}{100}Third. 
{1010}{1033}Fourth etc. 

3)從1使輸出)和2),得到既字幕(簽字*)原文:

{0}{20}First subtitle 
Translation of the first subtitle. 
{30}{50}Second subtitle|New line is made this way. 
Translation of the second subtitle.|Second line of translation of the second subtitle. 
{70}{100}Third. 
Translation of third. 
{1010}{1033}Fourth etc. 
Translation of fourth etc. 

可有人請給我一些建議如何開始?非常感謝。

我也許應該提到(這應該是清楚的),我會打電話給它這樣的:

cat input_file.txt | sed <"program" in sed> 
+0

[作業標籤已被棄用,不應再使用。](http://meta.stackexchange.com/questions/147100/trogdor-ate-my-homework-tag) – Lion

+2

什麼是「翻譯」?你有什麼嘗試?另外,你不需要在'sed'中使用'cat'。 –

+0

翻譯正在將一種語言的文本轉換爲另一種語言。 和你剛纔翻譯的書一樣,例如從法語到英語等。在這種情況下,我只用一種語言翻譯原文,並用其他語言翻譯文本。我在sed嘗試了另一項任務,但我不知道從哪裏開始。我知道我不需要使用貓 - 這只是如何運行它的一種方式。 – user1097772

回答

0

一旦保存字幕文件的翻譯file_1字幕文件file_2,執行命令:

sed -r 's/^[{][0-9]+[}][{][0-9]+[}]//' file_2 | paste -d"\n" file_1 - 
0

一個這樣做的方式:


步驟1和2:從分離出翻譯字幕和原來的字幕(*)(我叫它在下面的腳本sub_both

sed -r ' 
/^((\{[0-9]+\}){2}).*/ { 
    w sub_orig 
    s//\1/ 
    N 
} 
s/\n// 
w sub_tran 
' sub_both 

它所做的是:

  1. 匹配以大括號內的兩個數字序列開頭的行。
  2. 收件那些行到文件sub_orig
  3. 替換爲第一捕獲子表達式的線(這是數字2組的序列)
  4. 追加下一行(它是翻譯的線)到圖案空間。我們記得,3.之後的模式空間只是2位數字的序列。
  5. 刪除在圖案空間中的換行,導致{digits}{digits}translated line...
  6. 寫模式空間到文件sub_tran

步驟3:現在,我們有sub_origsub_tran,重建(*)作爲sub_both_2

paste -d "\n" sub_orig <(sed -r '/^((\{[0-9]+\}){2})//' sub_tran) >sub_both_2 

sub_tran is preprocessed v ia sed刪除2位數字序列,並將2個文件與換行符合併爲分隔符。

p/s:<(command)是進程替換,它從command創建臨時文件。