?
Solved

perl - compare and print if

Posted on 2007-10-18
6
Medium Priority
?
636 Views
Last Modified: 2011-09-20
I have a file with record entries seperated by new line like....


dn: cn=Alain Buisson  100111,ou=employee,o=xyz,c=xx
uid: alain-19850590
icsCalendar: alain-19850590

dn: cn=Alain Lecourt  100134,ou=employee,o=xyz,c=xx
uid: alain-19820986
icsCalendar: alain-19820000

dn: cn=Alain Mamessier  100345,ou=employee,o=xyz,c=xx
uid: alain-19720975
icsCalendar: alain-19720975

dn: cn=Alain Menez  100398,ou=employee,o=xyz,c=xx
uid: alain-19779408
icsCalendar: alain-19779408


from each record (set of 3 lines) I need to find the ones where the
uid and icsCalendar does NOT match.


So for this exampel I would get a result of


dn: cn=Alain Lecourt  100134,ou=employee,o=xyz,c=xx
uid: alain-19820986
icsCalendar: alain-19820000


Haven't been able to get it to come out right.


Thanks,
0
Comment
Question by:bt707
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:nbkbar7
ID: 20102373
#!/perl/bin -W
#

$file = "in.dat";

my $dn = "";
my $uid = "";

my $tag = "";
my $data = "";
my $uidData = "";
my $icsCalendarData = "";


open (FILE, $file);

while (<FILE>) {
      $line = $_;

      ($tag, $data) = split(":", $line);

      if ($tag =~ "dn") {
            $dn = $line;
      } elsif ($tag =~ "uid") {
            $uid = $line;
            $uidData = $data;
      } elsif ($tag =~ "icsCalendar") {
            if (not ($uidData eq $data)) {
                  printf("%s%s%s\n", $dn, $uid, $line);
            }
            $dn = "";
            $uid = "";
            $line = "";
      }
}

that help?
0
 

Author Comment

by:bt707
ID: 20103674
nbkbar7,

that pulls out some of them but also pulls out a lot of them that ARE that same that I don't want.

Not sure why yet, still working on that.

Thanks,
0
 
LVL 85

Expert Comment

by:ozo
ID: 20104650
perl -00ne 'print unless /uid:(.*)\nicsCalendar:\1\n/'  in.dat
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 85

Expert Comment

by:ozo
ID: 20104716
Do the lines that you don't wand have different whitespace around uid or icsCalendar?
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 20104783
perl  -00ne 'print unless /uid:\s*(.*)\s+icsCalendar:\s*\1\s*$/'  in.dat
0
 

Author Comment

by:bt707
ID: 20107685
ozo,

all the lines for icsCalendar and uid are the same on format and whitespaces, after running your one liner I seen the only issue I had was because of some case that was different between the two so added the "i" and works just fine.

That's just what I was trying to do using a one liner and the -00ne but couldn't get the compare to work, your one liner works perfect.

Appreciate it.

Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question