Solved

Replace  the specific position in a file

Posted on 2014-11-19
10
112 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use perl to insert into MySQL database 9 135
Perl string replace for refred url 9 62
ppm conversion to curl on a module install 8 82
File Find regex problem 4 62
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 …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now