LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2009, 05:51 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
substitute a pattern only if some other pattern is present


Hi all

I have this file
sandy is a writer
writer is a poet
sandy is a poet

i want the O/P to be

sandy was a writer
writer is a sandy
sandy was a poet

MEANS, wherever "sandy" is there, I want 'is' to be replaced by 'was'
please help
 
Old 06-26-2009, 06:17 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
where is your code?
 
Old 06-26-2009, 06:32 AM   #3
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by raghu123 View Post
Hi all

I have this file
sandy is a writer
writer is a poet
sandy is a poet

i want the O/P to be

sandy was a writer
writer is a sandy
sandy was a poet

MEANS, wherever "sandy" is there, I want 'is' to be replaced by 'was'
please help
you want to replace 'is' by was whenever it is
preceded by 'sandy' or writer.

you can do this with sed:

suppose the original file is input.txt
you can get a resulting file named output.txt by:

# sed 's/\(sandy \|writer \)is /\1was /g' input.txt > output.txt
 
Old 06-26-2009, 06:53 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
@Vonbiber: No, I think that the OP wants to change 'is' to 'was' only when the initial name is 'sandy'. (Perhaps sandy has passed away?)

If that's right, here's how I would do it using Perl:

Code:
perl -i.bak -ple 's/is/was/ if m/^sandy/' filename.txt
Replace 'filename.txt' with the name of your file. That should make the changes and save a backup as filename.bak.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM
last pattern with sed? xpto09 Linux - Newbie 6 10-04-2007 08:01 PM
XML Pattern Ellops Programming 0 04-18-2006 10:59 AM
Pattern Recognition trbdk3 Linux - Software 1 01-27-2005 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration