The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Search and replace entire line insania Shell Programming and Scripting 1 05-22-2008 03:45 PM
Perl: Search for string on line then compare numbers! Crypto Shell Programming and Scripting 2 01-21-2008 09:50 PM
how to insert line break + string in vi (search & replace ) umen Shell Programming and Scripting 1 06-08-2006 08:42 AM
Perl: Search for string then parse next line pondlife Shell Programming and Scripting 4 04-06-2006 01:44 AM
Search and replace multi-line text in files marz Shell Programming and Scripting 10 10-10-2005 05:05 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-03-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Perl: Search for string on line then search and replace text

Hi All,

I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text.

An example of 4 lines in my file is:

1. MatchText_randomNumberOfText moreData ReplaceMe moreData
2. MatchText_randomNumberOfText moreData moreData ReplaceMe moreData moreData
3. MatchText_randomNumberOfText moreData ReplaceMe moreData moreData
4. TextTextText_randomNumberOfText moreData ReplaceMe moreData moreData

The above is an example of four lines. I want to find all the lines containing "MatchText" (lines 1,2 and 3) and replace the "ReplaceMe" in that line with "REPLACED".

"ReplaceMe" occurs multiple times in lines I do not want to replace it in (as in line 4 above)

Hope that makes sense!!
Many thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-03-2008
photon's Avatar
Registered User
 
Join Date: Jul 2002
Posts: 131
loop through each line.

Code:
if($line =~ /MatchText/){
    $line =~ s/ReplaceMe/REPLACED/gi;
}
Reply With Quote
  #3 (permalink)  
Old 01-03-2008
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 758
Code:
perl -pi -e 's/ReplaceMe/REPLACED/ if /MatchText/' filename
Reply With Quote
  #4 (permalink)  
Old 01-03-2008
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 758
using sed:
Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename
Reply With Quote
  #5 (permalink)  
Old 01-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Thanks a million, great stuff
Reply With Quote
Google UNIX.COM
Reply

Tags
gnu sed, perl

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0