# all occurences of each line from a file in another file for each line in the first file

This is a code golf problem: Say you have two files, one file s and one file h. The problem is that for each line l of s you should produce a list that contains all lines of h that contain l. By "contain" I mean a substring, so, for example the line "foobar12baz" contains either foo, bar, foobar, 12, 1 or baz, etc...

You may use any programming language or any program to accomplish this in the least number of characters possible. Instead of a list you may print an array, or other sequence type structure.

Here is some Haskell I wrote that does it in 103 characters, and assumes you have the following modules imported

Data.List

Control.Monad

Control.Applicative

let f = (<$>) lines . readFile in (\(a,b)-> [[d|d<-b, isInfixOf c d]|c<-a]) <$> liftM2 (,) (f "s") (f "h")


Example files:

"h"

asdf1
asd2asdf
s3adsf


"s"

1
2
3


Output:

[["asdf1"],["asd2asdf"],["s3adsf"]]

• Thanks for the edits, Wes. And again, welcome to CodeGolf.SE. Mar 9 '12 at 22:38

## 3 Answers

just grep is enough...

grep -Ff s h


Better:

puts IO.readlines('h').grep /#{IO.readlines's'}/


In ruby, 53 characters, outputs an array and not a file:

p IO.readlines('h').each{|m|m=~/#{IO.readlines's'}/}


This reads file 'h' for every line of 's' to save a variable. Not ideal.

Happy golfing!

while read -r;do grep "$REPLY" --h>"$REPLY";done<s


YEAAAAAAAH!!1