|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
^ means start of the line, matching regardles of the characters' case is done by /i modifier.
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Does it have to be sed? Code:
perl -pe 's/(?<![a-z])jo(?![a-z])/bo/ig' file |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
![]() |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|