Solved

Replace  the specific position in a file

Posted on 2014-11-19
10
108 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
Comment Utility
perl -i -pe 's/(?<=^22.{8}).../yyy/;s/(?<=^77.{14}).../xxx/'   Inputfile
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#!/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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Getting help very quickly.I thought i accepted  solution .Sorry for the delay.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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 …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

9 Experts available now in Live!

Get 1:1 Help Now