grep a word from a specific line

for example:
searches only for single word
for single word
this is line three

match=$(grep -n -e "single" data.txt)

this command will stored "..... single ...... single" into search.

how can i grep the single word just from line 2 only??

If your requirement is to check if the word is present in a particular line , in a file, and assign the found word ( or the search word , as both will be same , if it is found ) then , I think this will work ,

team$ cat test
searches only for single word
for single word
this is line threeteam$

team$ cat testscript.ksh
#!/usr/bin/ksh

rc=`sed -n 2p test | grep "single" > /dev/null;echo $?`

if [ $rc -eq 0 ]
then
echo "Pattren found in 2 nd line"
echo " Assigning it to match variable"
match=single
else
echo "Pattern Not found in line 2"
fi

team$ testscript.ksh
Pattren found in 2 nd line
Assigning it to match variable
team@tst00972$

I gave a try ...will wait to see suggestions from experts ..:slight_smile:

awk 'NR==2 {
 for(i=1;i<=NF;i++){
    if ( $i == "single" ) {
       print $0
    }
 } 
}

i'm not sure i understood correctly but if you want to grep only on the second line, something like this might be helpful:

head -2 data.txt | tail -1 | grep "single"