Grepping text from one file in another file

Hello,
I have a file with a large number of words each listed in sequential order one word per line.

I want to search these words in another file which has the structure

Both the files are large, but the words in the sourcefile are all available in the target file.
I tried to grep the data using grep, but the older grep (8 bit) which supported such a procedure, is not supported in new grep.: grep 32 bit.
Maybe the syntax is different but I get the following:

Try as I will I have not been able to grep the file. Any suggestions and help for alternate methods: awk or Perl would be really welcome.
My OS is Vista/Win7 hence the request.

Not the slightest idea how grep on your system would work. Here you can find some ports of common GNU utilities. With those, grep -f patternfile should work.

1 Like

Hi.

There is a perl version of grep. Here is an example using your small data files:

#!/usr/bin/env bash

# @(#) s1	Demonstrate CPAN version of grep in perl.
# See:
# http://cpansearch.perl.org/src/CWEST/ppt-0.14/src/grep/tcgrep

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C tcgrep

EXPRESSIONS=data1
FILE=data2

pl " Expressions file $EXPRESSIONS:"
cat $EXPRESSIONS

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
tcgrep -f $EXPRESSIONS $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
tcgrep - ( local: RepRev 1.2, ~/bin/tcgrep, 2012-02-06 )

-----
 Expressions file data1:
a
b
c
d

-----
 Input data file data2:
a=x
b=y
c=p
e=q

-----
 Results:
a=x
b=y
c=p

See comments in script for URL to download perl code.

Best wishes ... cheers, drl

1 Like