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:

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! :slight_smile:

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

and:

sed 's/^am$/jo/i'
1 Like

hmm, that's not working the 'am' is still present in the file after it has ran? I have:

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 ^

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

works now cheers :b:

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

i see the ^ seems to overide differences between uppercase and lowercase?

^ means start of the line, matching regardles of the characters' case is done by /i modifier.

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

Does it have to be sed?

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

Hell no! :smiley:

is this able to differentiate between upper and lower case?

If you want to differentiate between upper and lower case, then ommit the "i" modifier. I think "i" also modifies behaviour of character range brackets, so A-Z needs to be added.

perl -pe 's/(?<![a-zA-Z])jo(?![a-zA-Z])/bo/g' file

with sed :wink:

# sed 's/JO[^ ]*\|jo/bo/' infile
akdjfdkl sdjfklsjdf jsdfkljsf bo KSJDFKDFJLJOdsjfskldjf dsjfkljsfl
akdjalsd@bo
sdfkjdslfjsl@dfdsf_bo
bo
bo

how do you string together multiple find and replaces, whenver I try and do it it only does the first one?

What do you mean? Do you want it to be case sensitive or not?:stuck_out_tongue: Give us sample input data, that covers all the cases and desired output.

perl -pe 's/(?<![a-z])jo(?![a-z])/bo/g;s(?<![a-z])tom(?![a-z])/bob/g' file

I just need to be able to specify multiple finds and replaces, the above does not work?

cheers!

Because you missed "/" in the second statement:

perl -pe 's/(?<![a-z])jo(?![a-z])/bo/g;s/(?<![a-z])tom(?![a-z])/bob/g' file

yeah noticed that before it's working now! what a dumbass (me) :rolleyes:

sed 's/JO[^ ]*\|jo/bo/' infile

how do you modify this sed line to replace uppercase with uppercase and lowercase with lowercase?