Grepping text from one file in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping text from one file in another file
# 1  
Old 11-05-2012
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.
Quote:
a
b
c
d
I want to search these words in another file which has the structure
Quote:
a=x
b=y
c=p
e=q
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:
Quote:
C:\Users\XP-HOME\Desktop>grep32
GREP Ver 1.0.0 (32 bit version for Windows NT)
Usage: grep [-bcilmnsvq? -dnnn] "pattern" file1 file2 ... [>ofile] [>>ofile]
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.
# 2  
Old 11-05-2012
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-05-2012
Hi.

There is a perl version of grep. Here is an example using your small data files:
Code:
#!/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:
Code:
% ./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
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

3. Shell Programming and Scripting

Help with grepping data from a text file

Hello, I have a text file which contains a list of strings which I want to grep from another file where these strings occur and print out only these lines. I had earlier used the grep command where File1 was the file containing the strings to be grepped (Source File) and File2 the Target File... (4 Replies)
Discussion started by: gimley
4 Replies

4. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

5. UNIX for Dummies Questions & Answers

Unexpected Behaviour from grepping Text File

Hi! I recently downloaded a wordlist file called 2of12.txt, which is a wordlist of common words, part of the 12dicts package. I've been getting unexpected results from grepping it, such as getting no matches when clearly there ought to be, or returns that are simply wrong. Par exemple: egrep... (4 Replies)
Discussion started by: sudon't
4 Replies

6. Shell Programming and Scripting

Grepping a file contents into another file

I have a file named as ucid.txt It has multiple rows of "id". I need to search and grep each line of it from a file named as pw_logs.txt and put the results into another file. Please help ! Thanks. (8 Replies)
Discussion started by: gopikrish81
8 Replies

7. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

8. Shell Programming and Scripting

Grepping from a point in a file to the end of the file

does any one know how to turn the equivalent of this command: awk '/2011 John Doe 8344/,0' /tmp/ops.log | egrep -c "received request" to something that would use egrep instead of awk? What the awk command does is, it searches the ops.log file for "2011 John Doe 8344". When it finds it,... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. Shell Programming and Scripting

Grepping a file based on input from a second file

how to grep a file based on another input file File1 ashu 1 ninetwo hari qwer 6 givefour jan fghj 8 noeight mar vbmi 7 noput feb -- --- File2 noput noeight --- -- Taking the input of grep as File2, a search need to be made in File1 giving File3 as output: (7 Replies)
Discussion started by: er_ashu
7 Replies

10. Shell Programming and Scripting

grepping all lines of one file from another file

Hi First post on here hope you can help with something I have a file with a couple of thousand lines (all lines are one string long, i.e a number I have another file that is over 1 million lines long Example entry from file 1 123456 Example from file 2 123456 mjhyuihbn ... (5 Replies)
Discussion started by: nampahc
5 Replies
Login or Register to Ask a Question