replacing a particular instance in a string globally


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replacing a particular instance in a string globally
# 1  
Old 08-16-2010
replacing a particular instance in a string globally

Hi,

I'm trying to update the last two characters coming in a string globally in a file.
Here is the sample data:

file1
Quote:
23456_r_o
test 1 tyu gh_o _odfgdfg
12345_otyuio ghtr
qwertyuiop_oghtd 1234_o tyu
vbghtyu ghd_o ghrtyu
In file1, I want to have all instances replace where _o is appearing in the end of a word with _g. If _o is appearing in the middle or any other position except the end, shouldn't get replaced.

So, the output should be:

Quote:
23456_r_g
test 1 tyu gh_g _odfgdfg
12345_otyuio _oghtr
qwertyuiop_oghtd 1234_g tyu
vbghtyu ghd_g ghrtyu
The below one liner isn't working as it replaces all instances of the above mentioned string.

Quote:
perl -wpl -i.bak -e 's/_o/_g/g' file1

How can I accomplish this i.e. replacing the instances where the instance of the string is coming at the end of each word.

Thanks
# 2  
Old 08-16-2010
You are very close. You need to include an 'end of word' marker so that it finds only the _o string at the end of the word.

Code:
sed -r 's/_o([ \t])/_g\1/g; s/_o$/_g/'

assumes that all words are terminated with either a space, tab, or newline. The use of the parens and \1 in the replacement, causes that part of the match to be used in the substitution; thus if a word ends in a tab, the replacement will be _g<tab>. If you have more characters that define the end of the word you can add them to the class with the space and \t.

Replace -r with -E on the command line if you are using a BSD flavour of sed, or are using sed from AT&T's AST distribution.
# 3  
Old 08-17-2010
Code:
perl -wpl -i.bak -e 's/_o$/_g/g' file1

But in your sample O/P, some _o followed with space is also updated.
Code:
 sed -e 's/_o$/_g/;s/_o /_g /' urfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep for a string until instance of a space

Hey guys, I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this. I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Discussion started by: terrell
6 Replies

2. UNIX for Advanced & Expert Users

Weird TR behavior. Replacing two instance

Can someone please explain what's wrong with the command i use below? tr -c '\11\12\40-\176' ' '< $TEMP_FILE > $TEMP_FILE2 The invalid character/s is replaced with two spaces, the string2 only have 1 space in it. Please help. Sample output: 333243,333244c333243,333244 < ... (1 Reply)
Discussion started by: Jin_
1 Replies

3. Shell Programming and Scripting

grep second instance of same string

Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times. <title>Tonight</title> <title>Thursday</title> <title>Friday</title>... (6 Replies)
Discussion started by: scubasteve39
6 Replies

4. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

5. Shell Programming and Scripting

awk - last instance of a string

How do I use awk to find the last occurence of a string in a file? (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

awk - find first instance of string after NR==10

How do I use awk to find the NR of first instance of a specific string after eg NR==10? I need to find the first instance of the word "class" after a specific NR. (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

8. Shell Programming and Scripting

find the first instance after a string

I have this file (below) and need to get out specific data that appears after OSE1_1.FIX, but before OSE1_2.FIX. Specifically I need to get all of the data after "ROW80_20:", "ROW80_21:", "ROW80_22:" & "ROW80_23:" then I need to do the same for data the appears after OSE1_2.FIX, but before... (2 Replies)
Discussion started by: josslate
2 Replies

9. Shell Programming and Scripting

Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it: Tue Jan 27 04:03:22 2009 : add session begin for mfgeb on /dev/pts/th. : Converting relative path to absolute path. : add session end. Tue Jan 27 04:03:29... (6 Replies)
Discussion started by: dinesh1178
6 Replies

10. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies
Login or Register to Ask a Question