Help a newb with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help a newb with sed?
# 1  
Old 09-16-2009
Question Help a newb with sed?

So here is my problem:

I need to do a sed search and replace on the following:

variable_blah_STATUS replaced with "replace string".

sed 's/variable_blah_STATUS/replace string/g' works great but there is a catch.

The catch is that I need to check and not replace variable_blah_STATUS if there is either a underscore before it or after it.

So:

_variable_blah_STATUS : do not replace
variable_blah_STATUS_ : do not replace
_variable_blah_STATUS_ : do not replace

variable_blah_STATUS : REPLACE!

Is there any easy way to do this with sed? I tried something with word boundaries using \b and it looked like it was just using it as part of the search string and it doesnt really help me with the whole underscore thing anyways. Obviously I think I need some regex stuff, but could not get it to work.

Thanks in advance for any help. It is greatly appreciated, this thing is driving me nuts.
# 2  
Old 09-16-2009
It seems to me \b works fine?
Code:
sed 's/\bvariable_blah_STATUS\b/replace string/g

What did not work for you?
Code:
echo "variable_blah_STATUS _variable_blah_STATUS \
variable_blah_STATUS_ _variable_blah_STATUS_"\
|sed 's/\bvariable_blah_STATUS\b/replace string/g'

replace string _variable_blah_STATUS variable_blah_STATUS_ _variable_blah_STATUS_


Last edited by Scrutinizer; 09-16-2009 at 08:21 PM..
# 3  
Old 09-16-2009
Code:
sed 's/\([^_]\)variable_blah_STATUS\([^_]\)/\1replace string\2/g' myFile

naaaah- not yet.......
# 4  
Old 09-17-2009
Thanks so much for your help, however neither of those work for me. I am running on Sun Solaris if that makes any differance and my sed is from 2005 it says. Tried gsed as well to no avail. Any suggestions?

Code:
mcs -p /usr/bin/sed
/usr/bin/sed:
@(#)SunOS 5.10 Generic January 2005

Code:
echo "variable_blah_STATUS _variable_blah_STATUS \ variable_blah_STATUS_ _variable_blah_STATUS_" 
| sed 's/\([^_]\)variable_blah_STATUS\([^_]\)/\1replace string\2/g'
variable_blah_STATUS _variable_blah_STATUS
variable_blah_STATUS_ _variable_blah_STATUS_

Code:
echo "variable_blah_STATUS _variable_blah_STATUS \ variable_blah_STATUS_ _variable_blah_STATUS_" 
| sed 's/\bvariable_blah_STATUS\b/replace string/g'
variable_blah_STATUS _variable_blah_STATUS
variable_blah_STATUS_ _variable_blah_STATUS_

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

newb at scripting how would I do this?

Hi guys. I have been lurking for quit some time attempting to learn on my own but I have reached an impasse. I need to extract specific details from from a text file that I get and parse it out into separate files with the name of each individual and the date it was proccess. Then within... (2 Replies)
Discussion started by: lostpacket
2 Replies

2. HP-UX

Newb Help, need to image one HD to another

Hello, I am totally new to Unix We have a piece of test equipment, three of them actually, that run HP-UX 9.0 from like 1994. We had backup tapes with procedures to load the OS and our test software, but the 16-track tapes are corrupt now. HP will not support it, they dont have it anymore,... (11 Replies)
Discussion started by: Newball80
11 Replies

3. Shell Programming and Scripting

SED Newb - Range Rage!

Hi all First time poster long time lurker. I've been trying to get my head around SED but I think my beginner status is starting to prove too great a hindrence. I have nearly 100 CSS files that I need to modify in such a way that label, b, p .text{ some style stuff } would become b ,p... (9 Replies)
Discussion started by: GoneShootin
9 Replies

4. Shell Programming and Scripting

newb help needed

hi all, im working on a script that needs to read in a file, search for a string of text within that file, and if the string is found, do something. so i know i need to use an if-then statement, but how do i test for the existence of a string within the file? any help would be greatly appreciated. (6 Replies)
Discussion started by: mdlearnest
6 Replies

5. UNIX for Dummies Questions & Answers

Newb here....Someone spying on me?

Hey all. I'm brand spankin' new here to the board. I'm also a novice at Unix. I've been using it for about 1 year now but I haven't really expanded my abilities since I learned the basics. I am in school and I mainly just use Unix to submit stuff to assignment directories and use it to compile my... (3 Replies)
Discussion started by: andre2000
3 Replies

6. UNIX for Dummies Questions & Answers

Ok I'm a Newb, Please Help

I am wanting to download the Linux Program. When i go to download it I see several things to download. What do i download exactly? Then what do I do to install it. Also I have partitioned my hard drive to make way for Linux on the other part to play with it, will any problems arise from this? I... (3 Replies)
Discussion started by: Seeto
3 Replies

7. UNIX for Dummies Questions & Answers

Newb question

I don't even know if this should go here but I just would like to know what this means: d0e45878043844ffc41aac437e86b602 I know absolutely nothin' about UNIX, and I found this in a SQL table in a board I run. Someone please tell me what that is in "normal" mode. Pardon me for my... (4 Replies)
Discussion started by: daeglin
4 Replies

8. UNIX for Dummies Questions & Answers

help for a newb

ok i just installed FreeBSD 4.8 on a computer i had lying around and it was working ok but then when i tried to set up KDE's xdm (kdm) i think i must have configured the login manager wrong because i cant login through the kdm and therefore i can log in to BSD at all?! please can someone help me... (5 Replies)
Discussion started by: h3x
5 Replies

9. UNIX for Dummies Questions & Answers

100% newb - please help

I run windows 2000 right now. I want to learn how to use unix/linux. How do i go about installing Unix/Linux? What's the easiest version to install? Where can i get it? Is my Hardware Compatable? How do i partition my Hd? HELP!!!!!:confused: (6 Replies)
Discussion started by: Rawdawg
6 Replies

10. UNIX for Dummies Questions & Answers

The biggest newb ever...

Hi, all you unix people. I am a pretty advanced windows user, but I am curious about unix. Is there any reason I should attempt to acquire some form of unix for my home computer system? What sort of things is unix useful for? Unix is open source, right? Assuming that to be the case, I infer that... (3 Replies)
Discussion started by: BoneMalone
3 Replies
Login or Register to Ask a Question