Solved

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

Posted on 2011-09-25
9
220 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
  • 3
  • 3
  • 3
9 Comments
 
LVL 11

Expert Comment

by:tel2
Comment Utility
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
Comment Utility
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 11

Expert Comment

by:tel2
Comment Utility
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
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:nnemo33
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
$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 11

Expert Comment

by:tel2
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

15 Experts available now in Live!

Get 1:1 Help Now