Perl parsing question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl parsing question
# 1  
Old 07-07-2009
Perl parsing question

I need some help loading an array. I have two unique delimiters, but I keep running into recursion.

Code:
#!/usr/bin/perl 

$INFILE="/root/scripts/data.txt";
$pat1="SCRIPT####";
$pat2="SCRIPT#echo";

$flag=0;
$inc=0;
$chunk="";

open(INFILE,"<$INFILE")|| die;
while(<INFILE>) {
    if (/$pat1/ ... /$pat2/) {
        next if $_ =~ /$pat1/;
        next if $_ =~ /$pat2/;
        #print "$_";
        $chunk .= $_;
        }
        @parsed[$inc] = $chunk;
        $inc++;
}

I've tried this with nest if loops and moved the various components around.

Here is a snippet of the data

Code:
SCRIPT########################LIST_MODULES####################
lsmod
Module                  Size  Used by    Not tainted
tg3                    49952   2 
SCRIPT#echo 'Done.'
SCRIPT########################5.5 ACULAB####################
 grep insmod /usr/local/etc/init.d/aculab_driver
do_insmod() {
  /sbin/insmod /usr/local/src/aculab/install/dacpswcl.o numcards=1 bustype=1 'serialorder="0: 0"' irqnum=15 ioaddry=0x394
 memwindow=0xD000 dmach=0 'clconfig="0: "' 'swconfig="0: "' 'smconfig=""' 
    do_insmod
SCRIPT#echo 'Done.'

Any help is appreciated.
# 2  
Old 07-08-2009
What is the o/p you expecting?
# 3  
Old 07-08-2009
I want to capture everything between SCRIPT#### and SCRIPT#echo. So this:

SCRIPT########################LIST_MODULES####################
lsmod
Module Size Used by Not tainted
tg3 49952 2
SCRIPT#echo 'Done.'
Becomes:

Code:
lsmod
Module                  Size  Used by    Not tainted
tg3                    49952   2



---------- Post updated at 12:38 PM ---------- Previous update was at 10:30 AM ----------

I figured out my problem. Here is the fix:

Code:
#!/usr/bin/perl 

undef $/;
my $file = <>;
my @parsed;

while ($file =~ /SCRIPT####(.*?)SCRIPT#echo/sg) {
    my $chunk = $1;
    push @parsed, $chunk;
}
foreach $x (@parsed) {
print "$x\n";
print "++++++++++++++++++++++++++++++++\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another parsing question (awk)

Input File Name of the session: filesrv_quo snap Logical Units UID: 60:06:01:60:01:7B:25:00:C8:86:B0:CA:5B:A2:E0:11 Name of the session: verspn2_at_176_0218 snap Logical Units UID: Name of the session: DRT-ny-iadsql1-c_ny-iadsql2-c snap Logical Units UID: ... (4 Replies)
Discussion started by: greycells
4 Replies

2. UNIX for Advanced & Expert Users

Perl parsing help required.

Hello, I got a file like this. 5201 5202 5203 5204 1234 2345 3456 4567 6210 6220 6230 6240 The required output should be 5201 1234 6210 (9 Replies)
Discussion started by: suverman
9 Replies

3. Shell Programming and Scripting

another parsing question

Input File Information about each HBA: HBA UID: 20:00:00:00:C9:9A:62:88:10:00:00:00:C9:9A:62:88 Server Name: 20:00:00:00:C9:9A:62:88:10:00:00:00:C9:9A:62:88 Server IP Address: UNKNOWN HBA Model Description: HBA Vendor Description: HBA Device Driver... (2 Replies)
Discussion started by: greycells
2 Replies

4. Shell Programming and Scripting

Parsing information in perl

So i'm trying to write a perl script that logins into a network switch via ssh: #sh ip traffic IP statistics: Rcvd: 1460119147 total, 563943377 local destination 0 format errors, 0 checksum errors, 48401998 bad hop count 0 unknown protocol, 8379279 not a gateway ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

5. Shell Programming and Scripting

Perl script parsing Help

Hi All, I am looking for a parsing in perl script which will parse DJEČJI SVIJET. There is a unicode character above character C. How to parse the total DJEČJI SVIJET in perl. Regards, Harikrishna (3 Replies)
Discussion started by: Harikrishna
3 Replies

6. Shell Programming and Scripting

Parsing question.

Hi There, First time poster here. I've got a parsing question and have a solution but am sure there is a much better way of doing this with just awk. My knowledge of awk is pretty limited so hope someone out there can give me a better solution. Here's the problem, I'm receiving a file from a... (2 Replies)
Discussion started by: little_happosai
2 Replies

7. Shell Programming and Scripting

Perl question, parsing line by line

Hello, Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands. Ive tried many methods however I cant get the last step. $yourfile= 'FILENAME';#poker hands to parse open (FILE, "$yourfile") or die $!; @lines = <FILE>; for (@lines) ... (1 Reply)
Discussion started by: Ek0
1 Replies

8. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

9. Shell Programming and Scripting

Parsing question

Hi Guys, I was wondering if you could help me out - I have a directory /home/users/datafiles/ which contain files "dat dd-mm-yy.xls" I am trying to write a script which does the following - (1) loops through all the files (2) retrieves the dd-mm-yy string and converts it into a... (12 Replies)
Discussion started by: muser
12 Replies

10. UNIX for Dummies Questions & Answers

Text parsing question

How would I split a file based on the location of a string, basically I want all entries above the string unix in this example 1 2 3 4 unix 5 6 7 Thanks, Chuck (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question