Solved

conditional replace using Perl

Posted on 2009-03-30
4
423 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delete query using perl dbi 3 100
How to strip .csv from file name 9 81
batch script for automated email 12 110
To run CGI/perl on tomcat and connect to oracle database. 5 126
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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