The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:45 PM
Perl: Search for string on line then compare numbers! Crypto Shell Programming and Scripting 2 01-22-2008 01:50 AM
how to insert line break + string in vi (search & replace ) umen Shell Programming and Scripting 1 06-08-2006 11:42 AM
Perl: Search for string then parse next line pondlife Shell Programming and Scripting 4 04-06-2006 04:44 AM
Search and replace multi-line text in files marz Shell Programming and Scripting 10 10-10-2005 08:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-03-2008
Crypto Crypto is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 5
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
  #2 (permalink)  
Old 01-03-2008
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
loop through each line.

Code:
if($line =~ /MatchText/){
    $line =~ s/ReplaceMe/REPLACED/gi;
}
  #3 (permalink)  
Old 01-04-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Code:
perl -pi -e 's/ReplaceMe/REPLACED/ if /MatchText/' filename
  #4 (permalink)  
Old 01-04-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using sed:
Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename
  #5 (permalink)  
Old 01-04-2008
Crypto Crypto is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 5
Thanks a million, great stuff
Sponsored Links
Closed Thread

Bookmarks

Tags
gnu sed, perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0