sed - replacing on the right of a pattern and looking for exact word? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

sed - replacing on the right of a pattern and looking for exact word?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-01-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
Lightbulb sed - replacing on the right of a pattern and looking for exact word?

how would you get SED to do the following, say you have the following lines in a text file:

user=tigger
some text some text
some text some text
some text some text
user=ted
some text some text
some text some text
some text some text
user=thekingofrockandroll

you want to find any line which has 'user=[the user name]' and replace it with 'user=xxx'

so the string after the 'user=' will always contain a different number of characters but you just want to sed to find any lines with 'user=' and replace the part after 'user=' with 'xxx' no matter what?

and...

if what to replace say 'AM' in a text file with 'JO', i can do:


Code:
sed 's/AM/JO/g;s/am/jo/g'

but what if the text file where i'm running this looks like this:

AM
TAME
am
jammy

I only want to replace the following lines with 'JO':

AM
am

so I want to leave 'TAME' and 'jammy' in there - the sed above will give this:

JO
TJOE
jo
JJOMY

I want:

JO
TAME
jo
jammy

how do you achieve that with SED?

Cheers!

Last edited by rich@ardz; 09-01-2010 at 11:07 AM.. Reason: Code tags :)
Sponsored Links
    #2  
Old 09-01-2010
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 August 2014, 5:25 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts

Code:
sed 's/user=.*/user=xxx/'

and:

Code:
sed 's/^am$/jo/i'

The Following User Says Thank You to bartus11 For This Useful Post:
rich@ardz (09-02-2010)
Sponsored Links
    #3  
Old 09-02-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
hmm, that's not working the 'am' is still present in the file after it has ran? I have:


Code:
sed 's/^am$/jo/i' "$f" > "$f".tmp && cp -f "$f".tmp "$f" && rm "$f".tmp

Using OpenSolaris 10 btw, not sure what version of sed i have? will that matter?

---------- Post updated at 04:40 PM ---------- Previous update was at 04:23 PM ----------

the $ was not needed, just the ^


Code:
sed 's/^am/jo/i' "$f" > "$f".tmp && cp -f "$f".tmp "$f" && rm "$f".tmp

works now cheers

---------- Post updated at 05:24 PM ---------- Previous update was at 04:40 PM ----------

i see the ^ seems to overide differences between uppercase and lowercase?
    #4  
Old 09-02-2010
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 August 2014, 5:25 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts
^ means start of the line, matching regardles of the characters' case is done by /i modifier.
Sponsored Links
    #5  
Old 09-03-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
hmm, what I would like ideally is this (say I'm replacing JO with BO)

my file to run sed on looks like this (as an example):

akdjfdkl sdjfklsjdf jsdfkljsf JO KSJDFKDFJLJOdsjfskldjf dsjfkljsfl <---- first JO on this line should be replaced by sed, second one should not because it forms part of a word
akdjalsd@JO <--- JO should be replaced here by sed
sdfkjdslfjsl@dfdsf_JO <----- and here
JO <---- and here
jo <----- and here
Sponsored Links
    #6  
Old 09-03-2010
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 August 2014, 5:25 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts
Does it have to be sed?

Code:
perl -pe 's/(?<![a-z])jo(?![a-z])/bo/ig' file

The Following 2 Users Say Thank You to bartus11 For This Useful Post:
rich@ardz (09-06-2010), usha rao (09-04-2010)
Sponsored Links
    #7  
Old 09-03-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
Quote:
Does it have to be sed?
Hell no!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace exact word with blank lavnayas Shell Programming and Scripting 2 11-10-2009 06:52 AM
Exact Word Match dinjo_jo Shell Programming and Scripting 2 10-13-2009 01:25 AM
regular expression for replacing the fist word with a last word in line kri_swami UNIX for Dummies Questions & Answers 2 07-31-2009 04:51 AM
exact string match in a word dr_sabz UNIX for Dummies Questions & Answers 11 12-15-2008 05:00 AM
Replacing a word after a matched pattern maxmave Shell Programming and Scripting 1 06-12-2008 03:54 PM



All times are GMT -4. The time now is 06:15 AM.