Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

conditional replace using Perl

Posted on 2009-03-30
4
Medium Priority
?
454 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
[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
  • 2
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 2000 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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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

636 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