aspose file tools*
The moose likes Perl and the fly likes Perl regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Perl
Bookmark "Perl regex" Watch "Perl regex" New topic
Author

Perl regex

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
How to compare two txt files String using perl regex
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11412
    
  16

Can you elaborate? usually you either want to compare two strings to each other, or you want to use a regex. I don't understand what you mean by 'use a regex to compare two strings'


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
hai.txt
-------
designation: Manager
name: David
age: 40

bye.txt
--------
designation: Manager
name: Alex
age: 41

I want two compare hai.txt and bye.txt files name and age (If designation is manager only then i need to compare name and age ) ? If designation is different then i need to create another text file and write designation, if name/age is different then i need to create another text file and write designation and name/age.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Does it have to be a Perl regular expression? I would use UNIX commands via a shell script.

1) grep for "designation: Manager"
2) if find a match
3) diff the 2 files


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
Detailed explanation:

hai.txt
-------
designation: Manager
name: David
age: 40


designation: Programmer
name: Bob
age: 32

designation: DBA
name: Bill
age: 50





bye.txt
--------
designation: Manager
name: Alex
age: 41


designation: Manager
name: David
age: 40


designation: Analyst
name: Mary
age: 45

designation: Programmer
name: Bob
age: 35


designation: DBA
name: Jack
age: 50


designation: Lead
name: Jim
age: 47



First perl routine should take hai.txt first entry designation and compare with bye.txt's all designation. If it does not match with any of the designation in bye.txt, it should write designation value in result.txt file like designation: Programmer does not match.
If designation matches, then it should check name.(eg: designation: DBA matches in both the file, but name Bill and Jack does not match. This should entered in another file result1.txt like designation: DBA name: Bill and Jack does not match.
Next level of comparison: If designation and name matches then it should compare age, if age differs entered in file result1.txt like designation: Programmer name: Bob age: 32 and 35 does not match.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Ok then using Perl does make sense. You still aren't going to be able to do this in a regular expression. Why not outline the implementation of what you plan to do (pseudocode or Perl code) and explain where you are stuck and asking about a reg expression. That will show what that reg exp should match.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
I am planning to solve without RegEx using Hashmap keys comparison. I am planning to separate the String value designation: Programmer as keys and values using Hashmap1 for first text file and Hashmap2 for second text file. Then comparing the Hashmap keys(key1 and Key2) using compare/diff. Is it right approach or any other better approach ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Perl regex