looking for a pattern from 1 file to another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers looking for a pattern from 1 file to another file
# 1  
Old 09-15-2008
looking for a pattern from 1 file to another file

I need to look for the lines that are similar to the original file.
for example

file1:
ass
asdfg
asdfge

file2:
werdfe
asx
asdfgpoi
vcx
asdfgem,.ko

outputfile:
asdfgpoi
asdfgem,.ko


im thinking of using grep
is it possible to use comm here?
# 2  
Old 09-15-2008
Carefully explain what you mean by 'similar'.
# 3  
Old 09-15-2008
like in the example
if file 2 contains common characters to file1 it will be outputted to file3 and disregard the rest

for example both of them starts with asdfg except that file2 contains asdfghjk
file 3 will contain asdfghjk
# 4  
Old 09-15-2008
I take you to mean - start at the beginning of the big string. IF the smaller string exists at that point it is a match
Code:
awk '{printf( "^%s\n", $0)}' file1 > t.grep
grep -f t.grep file2

Otherwise - if the little string exists anywhere in the big string
Code:
grep -f fil1 file2


Last edited by jim mcnamara; 09-15-2008 at 12:08 PM.. Reason: added (
# 5  
Old 09-15-2008
it works
thanks
but you misplaced ")" in your code
there shouldnt be any ")" there

question is why does file1 disappear?
# 6  
Old 09-15-2008
See the edit abopve printf() is an awk function. needs ()
# 7  
Old 09-15-2008
oh
but i got an error with it
so i deleted it
and it works

oh i see
i used copy paste
hehehe
kind of a lazy there

and now i can see the file1
weird

can you explain your code?
i tried searching the web for t.grep
but cant find anything
thanks anyway
you've been helping me a lot these days
im such a noob
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

2. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

6. Shell Programming and Scripting

Delete a pattern present in file 2 from file 1 if found in file 1.

I have two files File1 ==== 1|2000-00-00|2010-02-02|| 2| 00:00:00|2012-02-24|| 3|2000-00-00|2011-02-02|| File2 ==== 2000-00-00 00:00:00 I want the delete the patterns which are found in file 2 from file 1, Expected output: File1 ==== (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

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

9. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

10. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies
Login or Register to Ask a Question