grepping all lines of one file from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grepping all lines of one file from another file
# 1  
Old 06-23-2009
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 ukghyug uigyu yuyuf utyf rtr

I need to grep the second file for all lines that contain one of the lines from file 1

so at the end i will be left with a line from file 2 that coresponds to each entry in file 1

Any ideas?

Thanks
# 2  
Old 06-23-2009
man grep
look for -f option
# 3  
Old 06-23-2009
Quote:
Originally Posted by rakeshawasthi
man grep
look for -f option
Thanks for that, I had tried that but it didnt work i was doing

grep -f file1 file2

getting error
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .

any ideas, i must have usage wrong
# 4  
Old 06-23-2009
this should work.....
Code:
awk 'FILENAME=="file1"{A[$0]=$0}
FILENAME=="file2"{if($0 ~ A[$0]) {print}}' file1 file2

# 5  
Old 06-23-2009
Getting syntax error when I run that, It looks ok to me though,

Anyway i will stick at it, if i figure it out i will post up here

Thanks for your help
# 6  
Old 06-23-2009
Quote:
Originally Posted by nampahc
...
I need to grep the second file for all lines that contain one of the lines from file 1

so at the end i will be left with a line from file 2 that coresponds to each entry in file 1
...
Code:
$
$ cat file1
1
5
3
$
$ cat file2
1 this line has 1
5 this line has 5
4 this line has 4
2 this line has 2
3 this line has 3
$
$ perl -ne 'BEGIN {open(F1,"file1"); while(<F1>){chomp; $x{$_}=1} close(F1)}
           {split; print if $x{$_[0]}==1}' file2
1 this line has 1
5 this line has 5
3 this line has 3
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Grepping one file column from another file

Hi all, I want to search the second col of a file as a sub-part of 4th col of another file and produce a joint output. In the example, search if B is contained as a sub-part in E:B:C (sub-parts separated by colons). Note the second row is not found doesnt find a match as F isnt there in col 4... (19 Replies)
Discussion started by: newbie83
19 Replies

4. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

5. Shell Programming and Scripting

Grepping multiple lines in a file

HI I have a file with output as System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 I need to grep the values of following column fields u, s, w and i from each row sum them up and store in a variable..:( Please help.. (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question