• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Perl regex

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to compare two txt files String using perl regex
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic