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.


In reply to Re: Faster replacement of sed commands.. by aitap
in thread Faster replacement of sed commands.. by Ankur_kuls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.