0

grep コマンドで取得した行番号の配列を使用して、行番号を増やし、sed コマンドで新しい行番号にあるものを取得しようとしていますが、構文に問題があると想定しています (具体的には、他のすべてが機能するため、sed部分。)

スクリプトは次のとおりです。

#!/bin/bash


#getting array of initial line numbers    

temp=(`egrep -o -n '\<a class\=\"feed\-video\-title title yt\-uix\-contextlink  yt\-uix\-sessionlink  secondary"' index.html |cut -f1 -d:`)

new=( )

#looping through array, increasing the line number, and attempting to add the
#sed result to a new array

for x in ${temp[@]}; do

((x=x+5))

z=sed '"${x}"q;d' index.html

new=( ${new[@]} $z ) 

done

#comparing the two arrays

echo ${temp[@]}
echo ${new[@]}
4

2 に答える 2

1

これはあなたのために働くかもしれません:

#!/bin/bash


#getting array of initial line numbers    

temp=(`egrep -o -n '\<a class\=\"feed\-video\-title title yt\-uix\-contextlink  yt\-uix\-sessionlink  secondary"' index.html |cut -f1 -d:`)

new=( )

#looping through array, increasing the line number, and attempting to add the
#sed result to a new array

for x in ${temp[@]}; do

((x=x+5))

z=$(sed ${x}'q;d' index.html) # surrounded sed command by $(...)

new=( "${new[@]}" "$z" ) # quoted variables

done

#comparing the two arrays

echo "${temp[@]}" # quoted variables
echo "${new[@]}"  # quoted variables

あなたの sed コマンドは問題ありませんでした。で囲み、$(...)不要な引用符を削除して再配置するだけで済みました。

ところで

パターンの 5 行後の行を取得するには (GNU sed):

sed '/pattern/,+5!d;//,+4d' file
于 2012-10-07T07:20:53.247 に答える
0

あなたはおそらく次のようになるはずです:

z=$(sed - n "${x} { p; q }"  index.html) 

「-n」フラグを使用して、指定した行のみを印刷するようにsedに指示していることに注意してください。「x」変数に格納されている行番号に到達すると、それを出力し (「p」)、終了します (「q」)。x 変数を展開できるようにするには、sed に送信する commabd を単一引用符ではなく二重引用符で囲む必要があります。

また、後で使用する場合は、おそらく z 変数を二重引用符で囲む必要があります。

これが役立つことを願っています=)

于 2012-10-06T22:00:39.330 に答える