Leaving space inside sed

Hi Gurus,

I have 2 files

File1 :

veg apple
ap
pl
le
end

veg orange
or
an
ge
end

File2:

apple
orange

I had written a script to fetch data "veg" till "end"

for i in `cat file2.txt`
do
sed -n '/'veg $i'/,/end/p' file1.txt >> $i.txt
done

But when i execute this script, i am getting the following error "sed: -e expression #1, char 9: unterminated address regex"

Expected ouput:

veg apple
ap
pl
le
end
veg orange
or
an
ge
end

Can someone help me out with this ? How do i leave that space between "veg" and "$i" ?

Use double quotes to allow variable shell expansion :

sed -n "/veg $i/,/end/p" file1.txt > $i.txt

This will generate two files, hope that is what you wanted.

If not, substitute $i.txt with filename and put double redirection >>

Hope that helps
Regards
Peasant.

That worked !!!

Thanks a lot :slight_smile: