Grep the contents of a file with another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep the contents of a file with another file
# 1  
Old 10-16-2013
Oracle Grep the contents of a file with another file

hey guys, i got a easy one - i think.
i got 2 files:
file a.txt:
Code:
 
111
222
333

file b.txt:
Code:
 
dave the killer
TXTFF FGGD 222
herry  
FDSFG FDSFG 22
susan 
RHRHER 466
google 
FEEFF 686
heads night
RFHYH 111
tails   DGEGE 3337

so basicly what i m trying to do is to return the above line of b.txt for any match at a.txt

so for this example the result will be:
Code:
 
dave the killer
heads night

my problem is that i cant use grep -F since it's a sunos.
any help/advise will be appreciated. Smilie
# 2  
Old 10-16-2013
Have you tried /usr/xpg4/bin/grep?
This User Gave Thanks to CarloM For This Post:
# 3  
Old 10-16-2013
If the digit is always in the last field, you can try:
Code:
$ awk 'NR==FNR {_[$1]=$1; next} _[$NF] {print l} {l=$0}' f1 f2
dave the killer
heads night

If it could be anywhere in the line, try:
Code:
$ awk 'NR==FNR {_[$1]=$1; next} {for(f=1; f<=NF; f++){ if($f in _){print l}}} {l=$0}' f1 f2
dave the killer
heads night

If awk throws an error, you could try using /usr/xpg4/bin/awk instead.


If you go for what CarloM suggested, don't forget to look for the switch -f instead of -F and also if there is a switch like -B (= Before) to get the line before.
This User Gave Thanks to zaxxon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem to grep contents from a file

hi, I'm trying to grep content from one file in another file. The file that I'm searching into is large and hence I need to temporarily unzip it first. gzip -dc ALL.chr2.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz | grep '169997024\|190670539\|128051369' >... (2 Replies)
Discussion started by: janshamsani
2 Replies

2. Shell Programming and Scripting

need to grep contents of a file within specific time span. regex i am using is not working

Hi , I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 . i have tried two regex the first one seems to kind of work. it displays some output. the second one is... (13 Replies)
Discussion started by: chidori
13 Replies

3. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

4. Shell Programming and Scripting

Grep -v contents of a list file from another file.

Hello, I am trying to get output from FILE2 that excludes servers from list FILE1. I've tried a for loop with no success. Any ideas how to go about this? FILE1 contains: server1 server2 server3 server4 server5 FILE2 contains: server1:ERROR:user1: error message... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

7. Shell Programming and Scripting

How to grep the contents inside a tar file

Hi All I have searched the possibility of this options everywhere but am unable to find it in any forum. I have a tar file inside which there are n number of files and i dont know them. I need to grep a word inside the tar file and need to know in which file the word resides. > cat a... (2 Replies)
Discussion started by: Whiteboard
2 Replies

8. Shell Programming and Scripting

find file and print only contents with a hit by grep

Hi, can someone help me. I have some files and search a content in this files. If i have a hit I will print a output: filename:content But are more hits in one file: The output is always filename:content E.G. Seach about "three" file1 {one, two, three, four, three} file2... (5 Replies)
Discussion started by: Timmää
5 Replies

9. Shell Programming and Scripting

Copy subsequent contents of a file from first occurance of grep

There is a file which logs all errors and alerts of the database called alert log. I have a requirement as follows: 1. Check the current date and search for the first occurance of the current date in the alert log. 2. As soon as the first occurance is found, copy the subsequent contents... (5 Replies)
Discussion started by: sunpraveen
5 Replies

10. UNIX for Dummies Questions & Answers

how to strip out the contents of file using grep

Hi, I am receving a file from remote site which has EDI information for 830, 862 and 997 and I want to extect the data for 997 using grep's or any other methods. The data look like this: ISA~000 0000-0001-1000~997 AK1000~m 000~IEA~M ISA~000 0000-0001-1000~849 000~IEA~M ISA~000... (11 Replies)
Discussion started by: isingh786
11 Replies
Login or Register to Ask a Question