I don't see a solution with grep, but sed is an alternative to awk. With sed I would like to see patterns like b/foo/.* in 1.txt, but I will show a solution based on the (.*).
The purpose of the first command is constructing sed constructions, that will replace the inputline with the regular expression, when it matches the regular expression. The different output lines must look like
sed -rn 's#b/c/(.*)#b/c/#p' 2.txt
and this can be done with
# Use subprocess sed 's/\(.*\)\(([.][*])\)/s#\1\2#\1#p/' 1.txt # resulting in sed -rnf <(sed 's/\(.*\)\(([.][*])\)/s#\1\2#\1#p/' 1.txt) 2.txt| sort -u
The solution is a bit difficult to read, caused bij the layout of 1.txt, where I would want lines like b/foo/.*.
The above commands will have 2 bugs:
When the match is on a part of the line, the non-matched part will be shown in the output. This can be fixed with matching the garbage
# Use lines like 's#.*b/foo(.*)#b/foo#p' sed -rnf <(sed 's/\(.*\)\(([.][*])\)/s#.*\1\2#\1#p/' 1.txt) 2.txt| sort -u
The second bug is that strings in 2.txt that have two matches, will be matched only once (the first match will edit the line in the stream).
This can be fixed by adding some unique marker (I will use \a) for the matching lines and repeating the inputlines on the output (with \n&). The output can be viewed by looking for the \a markers.
sed -rnf <(sed 's/\(.*\)\(([.][*])\)/s#.*\1\2#\\a\1\\n\&#p/' 1.txt) 2.txt| sed -rn '/\a/ s/.(.*)/\1/p' | sort -u
EDIT:
The work-around with a marker and restoring the original input is not needed when you follow a different approach.
In sed you can print something to stdout without changing the stream.
One possibility (slow for this situation) is using
sed '/something/ eecho "something" '
Another possibility is using the "x" command (that eXchanges the pattern space with the hold buffer). You actuallu want to have a sedscript with commands like
\%a/% {h;s%.*%a/%p;x} \%b/% {h;s%.*%b/%p;x} \%b/c/% {h;s%.*%b/c/%p;x} \%b/foo/% {h;s%.*%b/foo/%p;x} \%d/% {h;s%.*%d/%p;x} \%e/% {h;s%.*%e/%p;x}
Using above method the sed solution simplifies into
sed -nf <( sed 's#([.][*])##; s#.*#\\%&% {h;s%.*%&%p;x} #' 1.txt ) 2.txt | sort -u
When the file 1.txt is not changed often, you might want to preprocess that file.
sed 's#([.][*])##; s#.*#\\%&% {h;s%.*%&%p;x} #' 1.txt > /tmp/sed.in sed -nf /tmp/sed.in 2.txt | sort -u
(.*)? (By the way, I don't think your "working solution" will work at all. I'm guessing you tweaked it between when you tried it and when you posted it here.)(.*)$at the end of all your patterns can I nly slow matching down. It has no impact on what is matched.b/foo/(.*)from 1.txt should matchb/foofrom 2.txt?