Search, compare or?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search, compare or?
# 1  
Old 09-28-2005
Lightbulb Search, compare or?

I found this very helpful forum and there are very friendly people, I have a small request.


I use AIX Unix and would like to know if there's a simple way how to do a kind of compare between two files.

I have 2 text files, let's name them file1 and file2.

What I need:

I want to read line by line file1 - certain field or better 2 fields of the file1 and look up the information in the file2.

To be more descriptive: I'd like to extract field1 at position 10-15 and field2 at position 25-31 from file1 and search or find if the same information is somewhere inside the file2. When no, I'd like to output the field1 & field2 off the file1 doesn't exit in file2.

let's say file1 contains something like:

123232 3232 2323 2323123123213 trterert

and file2 e.g.

123232 3232 2323 2323123123213 XXXXXXXX

and let's say I want 123232 & trterert from file1 to be searched in file2 ==> I get an "error" because the script would find only 123232 and not trterert.....

I'd be more than happy if someone can help me out or recommend a link or appropriate command or script.

I just need to check some record(s) in one file - and if they're available in the other file...

Please!

Thanks a lot!

Marian
# 2  
Old 09-28-2005
Code:
# put the first column and 
#   fifth column into a file, 
#   make it unique values

awk '{ print $1,$5 } ' file1 | sort -u > patternfile
#  loop thru the patterns from file1 
#  look for them in file2, print patterns if not found
while read pattern1 pattern2
do
    if [ ${#pattern2} -eq 0 ]; then  # skip when pattern2 isn't there
        continue
    fi
	grep "$pattern1" file2 | grep -q "$pattern2"
	if [ $? -ne 0 ]; then
		echo "$pattern1" "$pattern2"
	fi
done < patternfile

I'm assuming the fields from file1 can be anywhere on the line in file2.
# 3  
Old 09-29-2005
Thanks Jim,

what if I want to extract not columns but certain positions from file1? I mean I don't have any columns resp. tabs?

I'd like to choose e.g. positions 5-9 (let's say string1) and 23-35 (string2) from file1 and then search for it in file2 (whatever line with both string1 and string2 - doesn't matter at which position).

I hope you can 'fix' the script to reflect this Smilie

Many thanks for you kind help!

Marian
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

Search every line and compare fields

Hi All, I have situation where my file looks like this log.file. ID Start time IP SQL 1256152 05-Aug-15, 11:25:06 MST 10.54.20.33 Select * from TES 1004768 05-Aug-15, 11:25:06 MST 10.54.20.33 Select dummy 323323 05-Aug-15, 12:00:06 MST 10.15.20.77 ... (1 Reply)
Discussion started by: netdbaind
1 Replies

3. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

4. Shell Programming and Scripting

Search and compare files from two paths

Hi All, I have a 2 path, one with oldfile path in which has several sub folders,each sub folders contains a config file(basically text file), likewise there will be another newfile path which will have sub folders, each sub folders contains a config file. Need to read files from oldfile... (6 Replies)
Discussion started by: Optimus81
6 Replies

5. Shell Programming and Scripting

Search compare and determine duplicate files

Hi May i ask if someone know a package that will search a directory recursively and compare determine duplicate files according to each filename, date modified or any attributes that will determine its duplicity If none where should i start or what are those command in shell scripting that... (11 Replies)
Discussion started by: jao_madn
11 Replies

6. Shell Programming and Scripting

compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file File A 23 >pp_ANSWER 24 >aa hello 25 >jau head wear 66 >jss oops 872 >aqq olps ploww oww sss 722 >GG_KILLER ..... large files File B Beta done KILLER John Mayor calix meyers ... (5 Replies)
Discussion started by: cdfd123
5 Replies

7. Shell Programming and Scripting

Help with Compare and search content in directory

Hello, How to search in directory by comparing some string with the content of directory. Ex: I want to compare abhi string with the content of backup directory. i.e want to check that is there any file in backup directory having name ... (3 Replies)
Discussion started by: AbhijitIT
3 Replies

8. Shell Programming and Scripting

How to search & compare paragraphs between two files

Hello Guys, Greetings to All. I am stuck in my work here today while trying to comapre paragraphs between two files, I need your help on urgent basis, without your inputs I can not proceed. Kindly find some time to answer my question, I'll be grateful to you for ever. My detailed issue is as... (10 Replies)
Discussion started by: NARESH1302
10 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then compare numbers!

Hi All, I have a file that I need to be able to find a pattern match on a line, take the number on that line check if its >0.9 or <0.1 and if this is true write the line to output.out file. An example of 4 lines in my file is: 1. driver.I177.I11.net010 1.48622200477273e-05 2.... (2 Replies)
Discussion started by: Crypto
2 Replies

10. Shell Programming and Scripting

Search--Compare--Redirect

Hai Guru's, I have 3 files.A,B,C The file A,B is very huge.What i need to do is...I just get each record from file A and compare with File B...if the record presents...redirect the record to File C..otherwise not needed... Kindly give me ur valuable suggestions to resolve the above... (1 Reply)
Discussion started by: satheesh_color
1 Replies
Login or Register to Ask a Question