Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Perl regex

 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to compare two txt files String using perl regex
 
fred rosenberger
lowercase baba
Bartender
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic