LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-02-2010, 07:09 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
using bash sed to do the following


Assume I want to put all occurances of "ha" or "boowa" inside ::. so input "ha" gives output ":ha:".

I could do the following.
Code:
sed 's/\(ha\)/:\1:/g' test
sed 's/\(boowa\)/:\1:/g' test
But I want to use logical or and do it in 1 line of code.
sed 's/\(ha\) LOGICAL OR \(boowa\)/:\1: LOGICAL OR :\2:/g' test
Is there anyway to do that? The [] only only matches characters and not a string.
 
Old 12-02-2010, 07:27 AM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
So will this do what you want?

Code:
$ sed -e 's/\(ha\)/:\1:/g' -e 's/\(boowa\)/:\1:/g' file.txt 
:ha:
:boowa:
boo
foo
Regards,

Fordeck
 
Old 12-02-2010, 07:42 AM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by fordeck View Post
So will this do what you want?

Code:
$ sed -e 's/\(ha\)/:\1:/g' -e 's/\(boowa\)/:\1:/g' file.txt 
:ha:
:boowa:
boo
foo
Regards,

Fordeck
Thanks. Also how can I do a logical and? For example consider the following data in file called file.txt
Code:
Hey
'nt
've
'thou'
em'
last
Suppose I want to remove single quotes from the word only if it is surrounded by it.
So I'd only want 'thou' to be changed to thou. The following approach doesn't work (obviously)
Code:
sed "s/^'//" file.txt | sed "s/'$//" >result
So what I want is a logical and in the sed command. Thanks in advance.

Last edited by ghantauke; 12-02-2010 at 07:44 AM.
 
Old 12-02-2010, 07:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
So how about:
Code:
sed -r 's/(ha|boowa)/:\1:/g' file.txt
For your second one it would be soomething like:
Code:
sed -r "s/'([^']*)'/\1/g" file.txt
 
1 members found this post helpful.
Old 12-02-2010, 07:53 AM   #5
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
ignore this accidentally posted it

Last edited by ghantauke; 12-02-2010 at 07:59 AM.
 
Old 12-02-2010, 07:59 AM   #6
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by grail View Post
So how about:
Code:
sed -r 's/(ha|boowa)/:\1:/g' file.txt
For your second one it would be soomething like:
Code:
sed -r "s/'([^']*)'/\1/g" file.txt
Well thanks m8

Last edited by ghantauke; 12-02-2010 at 08:00 AM.
 
Old 12-02-2010, 08:04 AM   #7
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
This one will do it too:
Code:
sed -e "s/^'\(.*\)'/\1/g" file.txt

Last edited by angel115; 12-02-2010 at 08:05 AM.
 
Old 12-02-2010, 08:11 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
@angel115 - Although the shown input would work with your sed it would fail if more than one quoted item on a line (this is even if you remove the ^ so to look at others in the line)
Also, -r saves you worrying about escaping the brackets
 
Old 12-02-2010, 08:18 AM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Sorry I'm at work now on Windows, but wouldn't it work too?:

sed -r 's/ha|boowa/:&:/g' file.txt

or for exact matches (ie. 'ha' and not 'haaaa' or 'aha')

sed -r 's/\<ha\>|\<boowa\>/:&:/g' file.txt
 
1 members found this post helpful.
Old 12-02-2010, 10:27 AM   #10
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
This is the solution I've come up with.
Code:
sed "s/^'\([a-z']\)'$/\1/"
Seems to work perfectly. Thanks for all your help.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed in BASH ovince Programming 2 11-16-2007 12:55 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Bash, using variable in sed fur Programming 3 11-12-2005 07:41 AM
bash - sed/tr??? pk21 Programming 2 09-05-2003 07:56 AM
bash - sed pk21 Programming 6 03-07-2003 11:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration