Solved

Replace  the specific position in a file

Posted on 2014-11-19
10
115 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

809 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