2016-01-08 13 views
1

我有一個功能,應該讓之間的具體行號線,傳遞給它,並存儲在一個變量,並與模式「endhelp」猛砸行號和模式之間越來越線

下一行

代碼我現在有:

START_LINE=$1 #-- On which line the help is and where the search should start 

#-- Where the help command block ends 
END_LINE[1]=$(sed -n "$START_LINE,/endhelp/p=" filename) 

#-- Add one number to END_LINE as a second array value to speed line extracting 
END_LINE[2]=$((${END_LINE[1]}+1)) 

#-- The actual line extraction that outputs the whole lines 
sed -n "$START_LINE,${END_LINE[1]}p; ${END_LINE[2]}q" filename 

所以,如果我有這樣的事情:(注:有在輸入文件中其他類似的塊,所以這就是爲什麼起跑線上是很重要的)

-- some text -- 

help 
    text and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
endhelp 

-- some other text -- 

輸出將是:

text and some more text 
more words and text 
third help thing line 
stuff 
hi 

上述代碼是否可以工作,並且可以更有效地完成?還有如何使它停止,當它檢測到只有字符串'endhelp'的空行?

UPDATE

這裏是更新的代碼,做什麼,我想它做的事:

START_LINE=$2 #-- Where the help command block starts 
awk 'BEGIN {OUTPUT=0} NR=='$START_LINE' {OUTPUT=1} /^endhelp$/ {exit} OUTPUT' 

它停止,如果行有字符串「endhelp」,並開始從打印$ START_LINE。我添加了BEGIN {OUTPUT=0},因爲它在其他一些較舊的設備上發生了錯誤。

UPDATE2

我編輯的代碼再次,解決它退出,如果它看到「endhelp」上,否則空行它得到「START_LINE」前:

awk 'NR>='$START_LINE' {if ($0 ~ /^endhelp$/) {exit} else {$1=$1; print}}' 

它的體積更小並稍快一點。它還增加了$1=$1,可從當前行刪除尾隨和前導空白。如果不需要,它可以安全地移除。

+0

你傳遞給腳本的是哪條線? – anubhava

+0

「幫助」所在的行,所以它會從那裏開始打印 – BonBon

回答

3

它可以更有效地完成?還有如何使它停止,當它檢測到只有字符串'endhelp'的空行?

這裏是一個awk的版本會比你的腳本更高效:

awk -v n=$1 '/^endhelp$/{exit} p; NR==n || /^help$/{p=1}' file 

這將開始打印無論是從給定的行數,或者在一行中有剛help文本。它將繼續打印,直到出現endhelp文字爲止。此時awk將只是exit而文件的其餘部分將不會被處理。

+1

謝謝!我根據這個編輯我的代碼,它可以創造奇蹟! – BonBon

1

我寧願用awk oneliner提取線,你需要:

awk "NR==$1 && /help/ {flag=1;next}/endhelp/{flag=0}flag" filename 

輸入文件名和NR == 3:

-- some text -- 

help 
    text and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
endhelp 

-- some other text -- 

輸出:

text and some more text 
more words and text 
third help thing line 
stuff 
hi 

你也能做到這樣,只是指定的行號:

awk "NR==$1 {flag=1;next}/endhelp/{flag=0}flag" filename 
+0

我想過使用awk,這很好,但是應該開始的應該開始的行是給函數的。在輸入文件中可以有多個'幫助文本更多文本endhelp'段,所以只需從第一個'help'開始到'endhelp'就行不通了。我將在未來的答案中加入這個問題。 – BonBon

+0

您可以指定以 – tinySandy

+0

開頭的行號通過使用'awk'$'$ START_LINE',{flag = 1; next}/endhelp/{flag = 0}標誌'filename'? – BonBon

2

如果您已經在搜索起始行,爲什麼不在這些錨之間打印?

在Perl例如給出:

$ echo "$help_text" 
help 1 
    text 1 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
endhelp 

help 2 
    text 2 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
endhelp 

help 3 
    text 3 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
endhelp 

可以打印錨help \dendhelp之間的文字,像這樣:

$ echo "$help_text" | perl -0777 -ne 'print $1 if /^help[ \t]+3(.*?)^endhelp/ms' 

    text 3 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 

在awk中:

$ echo "$help_text" | awk ' 
> /^help 3/ {flag=1; next} 
> /^endhelp/ {flag=0} 
> flag {print}' 
    text 3 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 

如果您設置爲使用行號作爲塊的開始,您可以執行:

$ echo "$help_text" | awk ' 
NR==17 {flag=1; next} 
/^endhelp/ {flag=0} 
flag {print}' 
    text 3 and some more text 
    more words and text 
    third help thing line 
    stuff 
    hi 
+0

其他的方式它可以工作,但是如果行以'endhelp'開始,並且如果行只有字符串'endhelp',我希望它停止。但是,謝謝你讓我走向正確的方向。 – BonBon