• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

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
nnemo33
Asked:
nnemo33
  • 3
  • 3
  • 3
1 Solution
 
tel2Commented:
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
 
nnemo33Author Commented:
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
 
tel2Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
wilcoxonCommented:
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
 
nnemo33Author Commented:
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
 
wilcoxonCommented:
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
 
nnemo33Author Commented:
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
 
wilcoxonCommented:
$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
 
tel2Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now