Suppose I have one file

File1 consist two Words


Now i need to print all the lines of a file M starting from word PAPER end to word Team

sed command to print between two words is sed -n "/PAPER/,/TEAM/p" file2

But thing is right now I am directly giving words but it should check itself to words everytime in file1 and grep the all lines starting from one word to end to other in other file2.

1 Answer 1

while read -r first second ; do 
    sed -n "/${first//\//\\\/}/,/${second//\//\\\/}/p" file2 ; 
done < file1

note, that ${first//\//\\\/} is used to replace possible / symbol to escape sequence \/ cause otherwise contruction may be broken.

if you're completely sure you don't have / symbol in file1 then you can use just $first and $second.

  • / is not the only problem character. ., backslash (also for read unless you use -r), ^, [, $, * also are. Nov 24, 2014 at 12:46
  • This doesnt solve my problem actually File one having two words those i need to search in file2 in your code i am not getting how it is getting first and second from file 1
    – yisha
    Nov 25, 2014 at 4:23
  • @yisha, these words are read with read in a while cycle.
    – rush
    Nov 25, 2014 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.