Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Data from a CSV file 1

Status
Not open for further replies.

Stel

IS-IT--Management
Oct 10, 2000
11
0
0
US
Greetings,

I'm new at PERL but I have scripting experience in a proprietery language (very c+ like but less type strict). I need a quick turn-around for the BOSS and can't take the time to dive deep into PERL the way I'd like to, so I'm turning to everyone for some help. PLEASE.

PERL is up and running on NT just fine. :)

I have a CSV file in rows that has been extracted from the Novell NDS. Below is a sample of that data. I want the results from the "Login Script" portion only if it has a #Capture in the value.

------------------------------------------------------------
"User Name","Object Container Name (DN)","Login Script Contents"

"Davismic","Anywhere.ABCDEF","#CAPTURE L=2 Q=.COL2007Q1.ABCDEF NB NFF"
"Dolced","Anywhere.ABCDEF","#CAPTURE L=2 Q=.COL2007Q1.ABCDEF NB NFF"
"Kaysert","Anywhere.ABCDEF","IF NETWORK_ADDRESS = "D4CFF853" THEN GOTO STOP"
&quot;Kaysert&quot;,&quot;Anywhere.ABCDEF&quot;,&quot;IF P_STATION<=&quot;00000000FFFF&quot; THEN GOTO STOP&quot;
------------------------------------------------------------
I've seen these files sizes to be several MB.

GOAL:
have output that has
&quot;User Name&quot; , &quot;Login Script&quot; if it has a #capture in the value.

My first thoughts were to try to match on the &quot;<line feed>&quot; string. I know that it is an OD OA in hex but can I use that in PERL?

Any direction is greatly. . . no GREATLY appreciated.


Thanks,
Stel [sig][/sig]
 
Code:
#!perl -w
# open an output file.
open(OPF, &quot;>junk.log&quot;) or die &quot;Failed to open output file, $!\n&quot;;

# open the log file for reading - your csv file.
open(IPF,&quot;<c:/temp/input.txt&quot;) or die &quot;Failed to open csv file, $!\n&quot;;

# while info come from the IPF handle...
while ($line = <IPF>)
{
$user = '';
$script = '';
# if the line contains the string 'CAPTURE'
if ($line =~ /CAPTURE/) 
    {
    # split the parts of the line into an array.
    @parts = split(/&quot;,&quot;/,$line);
    
    # throw away quotes - a little simple pattern matching
    $user = $parts[0]; # dereference array element
    $user =~ s/&quot;//g;   # replace &quot; with null
    
    $script = $parts[2];    
    $script =~ s/&quot;//g;
    
    # print first and third array elements to output file handle
    print &quot;USER - $user, LOGIN SCRIPT - $script&quot;;
    print OPF &quot;USER - $user, LOGIN SCRIPT - $script&quot;;
    }
}
close IPF;
close OPF;
This is a little tedious....could be more concise. But, I left it exploded, so to speak to make the process steps more obvious. Admittedly, the output may need a little cleaning up, but, hopefully, this gives you the chunks you need to get where you need to go.... ' Hope this helps...

[sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
This worked great!!! You were a big help.

Thanks.
Stel [sig][/sig]
 
This really REALLY helped me today. I was able to bring a solution to the table. No, I didn't take the credit for writing the script, but I did take credit for being able to find a solution to the problem. I can't thank you enough. The only way I know how is to keep checking the NT forum and offer my expertice there. That's what I live and breath 40+ a week.

Thanks again,

Stel [sig][/sig]
 
Glad to help. Thanks for the thanks and the star;^) [sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top