Solved

Replace  the specific position in a file

Posted on 2014-11-19
10
119 Views
Last Modified: 2014-11-27
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
0
Comment
Question by:steve wa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40454184
perl -i -pe 's/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/'   Inputfile
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40454294
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
 

Author Comment

by:steve wa
ID: 40454876
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

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

Author Comment

by:steve wa
ID: 40455665
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40455694
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40455702
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
 

Author Comment

by:steve wa
ID: 40455824
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40455886
Oops.  The places that have $ARGV[1] should be $ARGV[0] ($0 = program name but $ARGV[0] = first argument passed in).
0
 

Author Closing Comment

by:steve wa
ID: 40468999
Getting help very quickly.I thought i accepted  solution .Sorry for the delay.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question