Replace the specific position in a file

Need to read the file each line and  replace  the specific position if  first 2 position have specific value .

Inputfile  before modification

58855kkdlfldfddkdkdkkdkdkdk
22testingperl
565gglrphdhdjjskkskllslseee
77testingcontext123read

Inputfile  after modification

If position 1-2=22  replace  position 10-13 with 'y'

If position 1-2=77 replace  position 17-19 with 'x'




58855kkdlfldfddkdkdkkdkdkdk
22testingnyyy
565gglrphdhdjjskkskllslseee
77testingcontextxxxread
steve waAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
perl -i -pe 's/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/'   Inputfile
0
wilcoxonCommented:
I notice one problem with your example and ozo's code.  You say if 22 then position 10-13 which is 4 characters (not 3) so the second line should be 22testingyyyy.

I assume you are using base-1 indexes (eg first character = 1) and not typical perl base-0 (eg first character = 0)?

If so, alternately:
perl -i -pe 'if (substr($_, 0, 2) eq "22") { substr($_,9,4) = "yyyy" } elsif (substr($_,0,2) eq "77") { substr($_,16,3) = "xxx" }'

Open in new window

0
steve waAuthor Commented:
Thank you for correction in my example can I  put the code inside .pl script and passing inputfile as parameter?
./Script.pl  test.txt



Code provided $ argv [0]
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ozoCommented:
#!/usr/bin/perl -i.bak -p                                                                                                    
s/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/  # modulo corrections in your example
0
steve waAuthor Commented:
I used the code provided by willcoxon.

Running like this.
Perl test.pl inputfile

Got the following error

do u need to predeclare pe?
String found where operator expected
Scalar found where operator expected
0
wilcoxonCommented:
Sure.  Personally, I'd write it differently as a script than a one-liner but it works fine either way (as evidenced by ozo's converted version).
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
tie my @lines, 'Tie::File', $ARGV[1] or die "could not tie $ARGV[1]: $!";
foreach (@lines) {
    if (substr($_, 0, 2) eq "22") {
        substr($_, 9, 4) = "yyyy"
    } elsif (substr($_, 0, 2) eq "77") {
        substr($_, 16, 3) = "xxx"
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wilcoxonCommented:
My original answer was for a one-liner run from the command-line.  You could do the minimal modifications (see below) indicated by ozo's answer or you could use the alternate script I provide above.
#!/usr/bin/perl -i -p
if (substr($_, 0, 2) eq "22") { substr($_,9,4) = "yyyy" } elsif (substr($_,0,2) eq "77") { substr($_,16,3) = "xxx" }

Open in new window

0
steve waAuthor Commented:
Used the multi line codr using foreach

use of uninitialized value $ argv [1] in concatenation (·) or string at script.pl
Could not tie : no such file or directoru at script.pl
0
wilcoxonCommented:
Oops.  The places that have $ARGV[1] should be $ARGV[0] ($0 = program name but $ARGV[0] = first argument passed in).
0
steve waAuthor Commented:
Getting help very quickly.I thought i accepted  solution .Sorry for the delay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.