0

Using an array of line numbers acquired through a grep command, I'm trying to then increase the line number and retrieve what is on the new line number with a sed command, but I'm assuming something is wrong with my syntax (specifically the sed part because everything else works.)

The script reads:

#!/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[@]}

2 Answers 2

1

This might work for you:

#!/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

Your sed command was fine; it just needed to be surrounded by $(...) and have unnecessary quotes removed and rearranged.

BTW

To get the line five lines after a pattern (GNU sed):

sed '/pattern/,+5!d;//,+4d' file
Sign up to request clarification or add additional context in comments.

Comments

0

You're sed line should probably be:

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

Notice that we use the "-n" flag to tell sed to only print the lines we tell it to. When we reach the line number stored in the "x" variable, it will print it ("p") and then quit ("q"). To allow the x variable to be expanded, the commabd we send to sed must be placed between double quotes, and not single quotes.

And you should probably place the z variable between double quotes when using it afterwards.

Hope this helps =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.