Parsing a string in PERL


 
Thread Tools Search this Thread
Top Forums Programming Parsing a string in PERL
# 1  
Old 12-31-2008
Parsing a string in PERL

I have an extractfile (with fields delimited by pipes '|') and I want to prepend a counter based on the below requirements:
- The counter starts at 3.
- The counter increments only if the date (67th field of the extractfile) is different.

Below is what I started off with:
$cnt=2;
$date="dummy";
open( extractFile, "<$extractFile" ) or die "ERROR: Can't open file: $extractFile - $!\n";
open( outputFile, ">$outputFile" ) or die "ERROR: Can't write file: $outputFile - $!\n";
while( $extractLine = <extractFile> ) {
# if the date is the same then prepend the counter without incrementing it
print outputFile "$cnt|$extractLine";
# else if the date is different, then increment the counter and then prepend it
cnt++;
print outputFile "$cnt|$extractLine";
}

I want to avoid using the 'split' by pipes to read in the records because I will be dealing with 800 million record extract. So I want to make this process as efficient as possible.

To clarify, I need help in reading in a certain field (which is a date - 67th field) from a file without using the 'split' command.

Thanks,

- CB

Last edited by ChicagoBlues; 12-31-2008 at 08:24 PM..
# 2  
Old 12-31-2008
awk splits fields by default, so it is as good as it gets at doing that: try this
Code:
awk -F'|' ' BEGIN{cnt=3; old=""}
      {if (old != $67)
          { old=$67
            printf("%d|", cnt++)
          }
        print $0
       } ' inputfile > outputfile

But I don't understand why perl's split is off the menu. Did you try something to prove it doesn't work well?
# 3  
Old 12-31-2008
split is not very efficient in splitting 800 million records each having 100 fields.

- CB
# 4  
Old 12-31-2008
I think everyone would agree that splitting 800 million records is never a fast task. split might not be efficient, but regex sure are slower.

I would advise you to try and code a C app for that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

3. Shell Programming and Scripting

Need help on parsing string

for i in `cat list`;do lol=`curl -m 2 -s ${i} | grep 'class=info' | cut -d '>' -f14 | cut -d '<' -f1 | sed '/^$/d'`; if ;then echo "$i,$lol" >> dirty; echo "$i,$lol";fi; done cut: you must specify a list of bytes, characters, or fields Try `cut --help' for more information. it gave me that... (0 Replies)
Discussion started by: p33plime
0 Replies

4. Shell Programming and Scripting

String parsing

Hi, name=VDSL_TTV_ HN_SUB create coid=MA5603U phone=5678 portpolicy=APortSelectionPolicy rfu10=TTV rfu3=Dot1q sz7_portmode=VDSL2 rfu5=1234 srprofile.sy_profname=$ADSL_TTV_SubProfile1 I have a line like this. Its a single line.I need the output as name=VDSL_TTV_ HN_SUB create... (6 Replies)
Discussion started by: giri_luck
6 Replies

5. Shell Programming and Scripting

String parsing

Hi, name=VDSL_TTV_ HN_SUB create coid=MA5603U phone=5678 portpolicy=APortSelectionPolicy rfu10=TTV rfu3=Dot1q sz7_portmode=VDSL2 rfu5=1234 srprofile.sy_profname=$ADSL_TTV_SubProfile1 I have a line like this. Its a single line.I need the output as name=VDSL_TTV_ HN_SUB create... (1 Reply)
Discussion started by: giri_luck
1 Replies

6. Shell Programming and Scripting

Parsing String

Hello All, I have a case, wherein I have a string of the format "attr1=value1 attr2=value2 attr3=value3 attr4=value4" How do I extract the value associated with for a given attributename. For eg. I need to get a value of "value2" when I give an input for attribute name as "attr2". Note, each... (10 Replies)
Discussion started by: msgforsunil
10 Replies

7. Shell Programming and Scripting

String-parsing!

I need the perl solution for the following : $string="I LOVE INDIA" now, in a new string i need the first character of each word... that is string2 should be "ILN". (10 Replies)
Discussion started by: vijay_0209
10 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. 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

10. Shell Programming and Scripting

Need help parsing a string

Hi, I'm writing a shell script that outputs, among other things, some of the information that is outputted by the mysqladmin status command. The output of the command looks like this: Uptime: 816351 Threads: 19 Questions: 80719739 Slow queries: 1419 Opens: 15903523 Flush tables: 1 Open tables:... (6 Replies)
Discussion started by: achieve
6 Replies
Login or Register to Ask a Question