Solved

conditional replace using Perl

Posted on 2009-03-30
4
402 Views
Last Modified: 2012-05-06
I have a body of text and I want to do a conditional replace based, I am assuming with an if statement.

text example:  So I want to replace part of line 3, the attribute name, which is  "Significance", with a concatenation of line 1 value, in this case, "AtPID LR score" + "Significance".  How do I do this?

line1 <attr name= "mref" value = "AtPID LR score" index = "0" />
line 2<attr name= "Method" value = "AtPID LR score"  index = "0" />
line 3 <attr name= "Significance" value = "1248.12"  index = "0"/>
0
Comment
Question by:MichelleLacy
  • 2
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 500 total points
ID: 24020464

while(<>) {

    if($. == 1) {my $value = /value\s*=\s*"(.*?)"/;}

    elsif($. == 3) {s/name\s*=\s*"(.*?)"/name="$value$1"/;}

}

Open in new window

0
 

Author Comment

by:MichelleLacy
ID: 24020612
I am very new to perl, so would you walk me through this a little.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24020734
What I gave was the code to do what you asked - it is not a complete perl program.  Below is a complete program.  Save it to a file.  If you are on windows, it will have to have a ".pl" extension.  On unix, it can be named whatever you want.

To use it, pass the name of the input file on the command line.  Redirect the output to the new file.

For example, if you saved the below code to "dorename.pl", your original file was input.txt, and you wanted to have your new file named "output.txt", you would use this command:
    dorename.pl input.txt > output.txt

#!/usr/bin/perl

use strict;

use warnings;
 

my $value;

while(<>) {

    if($. == 1) {$value = /value\s*=\s*"(.*?)"/;}

    elsif($. == 3) {s/name\s*=\s*"(.*?)"/name="$value$1"/;}

    print;

}

Open in new window

0
 

Author Closing Comment

by:MichelleLacy
ID: 31564396
thank you.  I used it right away, but forgot to close the session.
0

Featured Post

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!

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 …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

23 Experts available now in Live!

Get 1:1 Help Now