Solved

Search and replace two string on a text file with two value

Posted on 2011-09-25
9
257 Views
Last Modified: 2012-05-12
I have text file such as:
_data m:0x5747374
          11 22 33 44
          44 55 33 22
          12 44 55 11
_code m:0x654343
          11 22 33 44
          44 55 33 22
          12 44 55 11
I need to change m:0x5747374 => value1 m:0x654343=> value2 using perl script. value after m: is hex value may be change and not limit about length.
Please support me with a script.
Thanks

0
Comment
Question by:nnemo33
[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
  • 3
  • 3
  • 3
9 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 36596907
Hi nnemo33,

It's a bit hard to be 100% sure of what you mean, so to make it easy, please provide a sample output file for each sample input file.

Thanks.
tel2
0
 

Author Comment

by:nnemo33
ID: 36596917
ok, here is a sample output:
_data m:0xAAAAAA
          11 22 33 44
          44 55 33 22
          12 44 55 11
_code m:0xCCCCCC
          11 22 33 44
          44 55 33 22
          12 44 55 11

0xAAAAAA<=> value1, 0xCCCCCC<=> value2 with value1, value2 are agruments pass for this function.
0
 
LVL 12

Expert Comment

by:tel2
ID: 36596991
Thanks for that, nnemo33.

Questions:

1. Is the following 1 file or 2 files?:
_data m:0x5747374
          11 22 33 44
          44 55 33 22
          12 44 55 11
_code m:0x654343
          11 22 33 44
          44 55 33 22
          12 44 55 11

2. Please explain carefully how this inpuit:
_data m:0x5747374
should get translated to this output:
_data m:0xAAAAAA
and this input:
_code m:0x654343
should get translated to this output:
_code m:0xCCCCCC

Please make this very clear, or I will give up and leave it so someone else.

Thanks.
tel2
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 26

Expert Comment

by:wilcoxon
ID: 36597213
I think this will do what you want (pending further explanation in response to tel2)...

Called as:

script.pl value1 value2 < input_file > output_file

It does the substitution based on value1 replaces the m:hex after _data and value2 goes after _code.

To match your example, it would be called as:

script.pl m:0xAAAAAA m:0xCCCCCC <input_file >output_file
#!/usr/local/bin/perl
use strict;
use warnings;

my $arg1 = shift || die;
my $arg2 = shift || die;

while (<>) {
    s{^(_data\s+)m:0x[0-9a-f]+}{$1$arg1};
    s{^(_code\s+)m:0x[0-9a-f]+}{$1$arg2};
    print;
}

Open in new window

0
 

Author Comment

by:nnemo33
ID: 36597540
Hi tel2,
thanks for your question
Question1: the data memory is a file
Question2: yes
Hi wilcoxon,
could you help me change source code to cover more, such as the pattern to search is same but it on a file:
_data m:0xAAAAAA
          11 22 33 44
          44 55 33 22
          12 44 55 11
_data m:0xCCCCCC
          11 22 33 44
          44 55 33 22
          12 44 55 11

The two pattern start with _data m:xxxxxx
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36599665
Here's a more general one that will replace any number of arguments specified on the command line in a file (all following _data m:hex)...

Called as:

script.pl value1 ... valueX <input_file >output_file
#!/usr/local/bin/perl
use strict;
use warnings;

my $cnt = 0;
while (<>) {
    if (m{_data\s+m:0x[0-9a-f]+}) {
        s{^(_data\s+)m:0x[0-9a-f]+}{$1$ARGV[$cnt]};
        $cnt++;
    }
    print;
}

Open in new window

0
 

Author Comment

by:nnemo33
ID: 36600057
Hi wilcoxon, could you write it into perl script (with input file is: mem.txt and output file is: output.txt) and explain for me meaning of {$1$ARGV[$cnt]}
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 36600457
$1$ARGV[$cnt] equals the first capture (_data\s+) from the lhs of the substitution followed by the $cnt'd argument passed in on the command line (eg value1 = $ARGV[0], value2 = $ARGV[1], etc).

I added a check to make sure that there was an argument specified before doing the substitution.
#!/usr/local/bin/perl
use strict;
use warnings;

open IN, 'mem.txt' or die "could not open mem.txt: $!";
open OUT, '>output.txt' or die "could not write output.txt: $!";

my $cnt = 0;
while (<>) {
    if (m{_data\s+m:0x[0-9a-f]+} and exists $ARGV[$cnt]) {
        s{^(_data\s+)m:0x[0-9a-f]+}{$1$ARGV[$cnt]};
        $cnt++;
    }
    print OUT $_;
}

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
ID: 36601777
Hi nnemo33,

Thanks for trying to answer my questions.

> Question1: the data memory is a file
I didn't ask whether it's a file.  I asked if it's "1 file or 2 files".  However, I now realise it's probably 1 file.

> Question2: yes
Yes/no answers don't work for questions like "Please explain carefully how...".

If you don't know English well nnemo33, then I suggest you get someone who does know English well to help you write your English questions and answers.

So, as promised in my last post, I will now give up (and leave this to wilcoxon, who seems to be doing well).

tel2
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans

726 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