Avatar of steve wa
steve wa
 asked on

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


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'


Avatar of undefined
Last Comment
steve wa

8/22/2022 - Mon

perl -i -pe 's/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/'   Inputfile

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

steve wa

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]
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

#!/usr/bin/perl -i.bak -p                                                                                                    
s/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/  # modulo corrections in your example
steve wa

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
steve wa

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

Oops.  The places that have $ARGV[1] should be $ARGV[0] ($0 = program name but $ARGV[0] = first argument passed in).
steve wa

Getting help very quickly.I thought i accepted  solution .Sorry for the delay.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck