Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Faster replacement of sed commands..

by aitap (Curate)
on Aug 19, 2014 at 10:19 UTC ( [id://1097957]=note: print w/replies, xml ) Need Help??


in reply to Faster replacement of sed commands..

For your simple sed commands, Perl equivalent can be produced by replacing sed -i with perl -pi -e. Having done this, we can use O 'Deparse' to get the actual Perl code produced by these switches:

$ perl -MO=Deparse -pi -e 's/GX_RAR_RAA_TRANSACTION/TRAR/g;s/GX_CCR_CC +A_TRANSACTION/TCCA/g;s/QUOTA_GRANTED/TQG/g;s/QOS_ASSIGNED_TO_DEFAULT_ +BEARER/TQA/g;s/RULE_INSTALLED/TRI/g;s/RULE_REMOVED/TRR/g' BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { s/GX_RAR_RAA_TRANSACTION/TRAR/g; s/GX_CCR_CCA_TRANSACTION/TCCA/g; s/QUOTA_GRANTED/TQG/g; s/QOS_ASSIGNED_TO_DEFAULT_BEARER/TQA/g; s/RULE_INSTALLED/TRI/g; s/RULE_REMOVED/TRR/g; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK
ARGV is a special filehandle which iterates over files supplied in command line arguments (@ARGV array).

Thus we can inline all your `perl ...` and `sed ...` into main program (untested):

#!/usr/bin/perl use Data::Dumper; use File::Copy 'move'; foreach my $file (</data/admin/scripts/SapcmedadpebM/xdecoder/decodedir/MHSA +PC/*csv>) { # work with files changed more than 10 minutes ago next if time - ( stat $file )[9] < 10 * 60; print scalar localtime; # instead of `date` { local @ARGV = ($file); # edit this file local $^I = ""; # enable in-place editing without ba +ckup LINE: while ( defined( $_ = <ARGV> ) ) { s/[^[:ascii:]]//g; tr/\015//d; s/QOS_PROFILE_ID/x1/g; s/CHARGING_PROFILE_ID/x2/g; s/CONTENT_FILTERING_PROFILE_ID/x3/g; s/SUBSCRIBERID/x4/g; s/RECORD_LENGTH/x5/g; s/RECORD_TYPE/x6/g; s/EVENT_ID/x7/g; s/EVENT_RESULT/x8/g; s/CAUSE_PROTOCOL/x9/g; s/DEFAULT_BEARER_ID/x0/g; s/ARP_PRIORITY_LEVEL/y1/g; s/ARP_CAPABILITY/y2/g; s/ARP_VULNERABILITY/y3/g; s/BEARER_CONTROL_MODE/y4/g; s/TRACKING_AREA_CODE/y5/g; s/ROUTING_AREA_CODE/y7/g; s/SERVICE_AREA_CODE/y8/g; s/SYSTEM_IDENTIFIER/y9/g; s/NETWORK_IDENTIFIER/y0/g; s/GX_RAR_RAA_TRANSACTION/TRAR/g; s/GX_CCR_CCA_TRANSACTION/TCCA/g; s/QUOTA_GRANTED/TQG/g; s/QOS_ASSIGNED_TO_DEFAULT_BEARER/TQA/g; s/RULE_INSTALLED/TRI/g; s/RULE_REMOVED/TRR/g; } continue { die "-p destination: $!\n" unless print $_; } } move( $abc1, "/data/admin/scripts/SapcmedadpebM/xdecoder/decodedir/compleat +/" ); }
We can also put all these substitutions in a hash to make code less repetitive:
#!/usr/bin/perl use Data::Dumper; use File::Copy 'move'; my %fields = ( QOS_PROFILE_ID => "x1", CHARGING_PROFILE_ID => "x2", CONTENT_FILTERING_PROFILE_ID => "x3", SUBSCRIBERID => "x4", RECORD_LENGTH => "x5", RECORD_TYPE => "x6", EVENT_ID => "x7", EVENT_RESULT => "x8", CAUSE_PROTOCOL => "x9", DEFAULT_BEARER_ID => "x0", ARP_PRIORITY_LEVEL => "y1", ARP_CAPABILITY => "y2", ARP_VULNERABILITY => "y3", BEARER_CONTROL_MODE => "y4", TRACKING_AREA_CODE => "y5", ROUTING_AREA_CODE => "y7", SERVICE_AREA_CODE => "y8", SYSTEM_IDENTIFIER => "y9", NETWORK_IDENTIFIER => "y0", GX_RAR_RAA_TRANSACTION => "TRAR", GX_CCR_CCA_TRANSACTION => "TCCA", QUOTA_GRANTED => "TQG", QOS_ASSIGNED_TO_DEFAULT_BEARER => "TQA", RULE_INSTALLED => "TRI", RULE_REMOVED => "TRR", ); my $regexp = "(" . ( join "|", map quotemeta, keys %fields ) . ")"; foreach my $file (</data/admin/scripts/SapcmedadpebM/xdecoder/decodedir/MHSA +PC/*csv>) { # work with files changed more than 10 minutes ago next if time - ( stat $file )[9] < 10 * 60; print scalar localtime, "\n"; # instead of `date` { local @ARGV = ($file); # edit this file local $^I = ""; # enable in-place editing without + backup LINE: while ( defined( $_ = <ARGV> ) ) { s/[^[:ascii:]]//g; tr/\015//d; s/$regexp/$fields{$1}/g; } continue { die "-p destination: $!\n" unless print $_; } } move( $file, "/data/admin/scripts/SapcmedadpebM/xdecoder/decodedir/compleat +/" ); }
I omitted the infinite loop and file creation. Do you need a lock file? Run perldoc -q lock to get instructions on proper locking: your current implementation may be prone to race conditions.

See also: localtime, quotemeta, $^I, local, perlfaq5.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1097957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found