LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2009, 07:23 AM   #1
Vilmerok
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
Question sed - regular expression


I need to extract substring :

my_string="foo bar(*.*)";

in variable1 should be "foo bar"
in variable2 should be "*.*"

So, everything text beetwen () should be in variable2.

How to create sed regexp for it?
 
Old 02-26-2009, 07:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
variable1=$(echo $my_string | sed 's/\(.*\)(.*/\1/')
variable2=$(echo $my_string | sed 's/.*(\(.*\))/\1/')
A must-reading to learn sed programming: http://www.grymoire.com/Unix/Sed.html
 
Old 02-26-2009, 07:33 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Homework?
 
Old 02-26-2009, 07:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pixellany View Post
Homework?
Oops... I didn't think about that... sorry!
 
Old 02-26-2009, 07:55 AM   #5
Vilmerok
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Code:
variable1=$(echo $my_string | sed 's/\(.*\)(.*/\1/')
variable2=$(echo $my_string | sed 's/.*(\(.*\))/\1/')
A must-reading to learn sed programming: http://www.grymoire.com/Unix/Sed.html
This works, but not correctly:

Code:
c="foo bar(*)"
e=`echo "$c" | sed 's/.*(\(.*\))/\1/'`                                                                                                                      f=`echo "$c" | sed 's/\(.*\)(.*/\1/'`

echo "$e"
echo "$f"

[@my myscript]$ ./script1.sh
*
foo bar
This is perfect!

But if c="foo bar"
[
Code:
@my myscript]$ ./script1.sh
foo bar
foo bar

But should be:
[@my myscript]$ ./script1.sh

foo bar
 
Old 02-26-2009, 08:44 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This should work:
Code:
e=$(echo $c | sed '/(/!d; /(/s/.*(\(.*\)).*/\1/')
it means "for lines not containing parenthesis delete the whole line" while "for lines containing parentheses extract the string within them".
 
  


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 regular expression Ammad Linux - General 7 10-29-2008 05:52 PM
regular expression (.*?) uttam_h Programming 6 05-30-2008 05:45 PM
sed regular expression help needed Dew Linux - Newbie 1 03-30-2005 02:59 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 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