Solved

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

Posted on 2011-09-25
9
228 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 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

21 Experts available now in Live!

Get 1:1 Help Now