perl - compare and print if

Posted on 2007-10-18
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.

Question by:bt707
    LVL 6

    Expert Comment

    #!/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?

    Author Comment


    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.

    LVL 84

    Expert Comment

    perl -00ne 'print unless /uid:(.*)\nicsCalendar:\1\n/'  in.dat
    LVL 84

    Expert Comment

    Do the lines that you don't wand have different whitespace around uid or icsCalendar?
    LVL 84

    Accepted Solution

    perl  -00ne 'print unless /uid:\s*(.*)\s+icsCalendar:\s*\1\s*$/'  in.dat

    Author Comment


    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now