Solved

conditional replace using Perl

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

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

Suggested Solutions

Title # Comments Views Activity
Perl troubleshooting error. Base64 bootstrap parameter 5 63
perl to convert excel to csv 3 182
instmodsh - listing perl modules 2 74
Perl Awk Need Help 3 109
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

22 Experts available now in Live!

Get 1:1 Help Now