meenac has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I have the following code to parse a csv file. A

Sample line on the file is
SEC, 9/12/2004, 14:25:18, Security, 576, Success, P +rivilege Use, NT AUTHORITY\SYSTEM, GHOST01, Special privile +ges assigned to new logon:^` User Name: GHOST01$^` Domain: + US^` Logon ID: (0x0 0x15307D9E)^` Privileges: + SeBackupPrivilege^` SeRestorePrivilege^` SeD +ebugPrivilege^` SeChangeNotifyPrivilege
foreach $line (<INPUT>) { chomp($line); ($logtype, $date, $time, $type, $eventid, $status, $systemevent, $nt +auth, $hostname, $description) = split(",", $line); # So, $description becomes #Special privileges assigned to new logon:^` User Name: GHOST +01$^` Domain: US^` Logon ID: (0x0 0x15307D9E)^` + Privileges: SeBackupPrivilege^` SeRestorePrivilege +^` SeDebugPrivilege^` SeChangeNotifyPrivilege # I just need to get the User Name which is GHOST01$ out. if ($eventid==576) { ($desc, $username) = split("`", $description); ($logon, $uname) = split(":", $username); print $uname; } }
But I get GHOST01$^. How do I remove the ^ char at
the end? Any suggestions?

2004-12-10 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Parsing problem
by dragonchild (Archbishop) on Dec 10, 2004 at 20:21 UTC
    split() delimiters can be any number of characters, so you could do
    ($desc, $username) = split("^`", $description);

    A few notes unrelated to your question:

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I believe that should be:
      ($desc, $username) = split("\^`", $description);
      since the first parameter to split is a RE, and the un-escaped ^ would indicate "start".

          ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

        Actually, it should be:

        ($desc, $username) = split(/\^`/, $description);

        since the first parameter to split is a RE and not an interpolated string. ;-)

Re: Parsing problem
by mpeters (Chaplain) on Dec 10, 2004 at 19:58 UTC
    chop($uname) if( substr($uname, length($uname) -1, 1) eq '^');
Re: Parsing problem
by NetWallah (Canon) on Dec 10, 2004 at 20:16 UTC
    Simple fix, using Regex:
    #REPLACE THIS LINE: ($logon, $uname) = split(":", $username); ##Old # With THIS line: ($logon, $uname) = split(/[:|^]/, $username); ##New
    Also, please use <code> tags when you post your code.

        ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld