我有一個功能,應該讓之間的具體行號線,傳遞給它,並存儲在一個變量,並與模式「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
,可從當前行刪除尾隨和前導空白。如果不需要,它可以安全地移除。
你傳遞給腳本的是哪條線? – anubhava
「幫助」所在的行,所以它會從那裏開始打印 – BonBon