LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-03-2013, 08:46 AM   #1
masonje
LQ Newbie
 
Registered: Mar 2013
Distribution: Ubuntu
Posts: 9

Rep: Reputation: Disabled
yet another sed expression question


I've been searching the forms for hours and haven't found a good example for how to do this sed regular expression.

Trying to automate the start-up/suspension/shutdown of VMs in Virtual box. The sed expression I need should only give me back the guid of the vm:

VBoxManage list runningvms | sed -e '(((insert expression here)))'

so of:

"DebianMC" {15154f73-4dbb-4fcf-b912-c973b16a36f8}

...I only want this:

15154f73-4dbb-4fcf-b912-c973b16a36f8

Much appreciated in advance!
 
Old 03-03-2013, 08:51 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Maybe this?
Code:
... | sed 's/.*{\(.*\)}/\1/'
In alternative you can use grep with a more strict regular expression, matching a string of 36 characters and made only of hexadecimal digits and hyphens:
Code:
... | grep -Eo '[0-9a-f-]{36}'

Last edited by colucix; 03-03-2013 at 08:56 AM. Reason: Added grep solution
 
1 members found this post helpful.
Old 03-03-2013, 09:07 AM   #3
masonje
LQ Newbie
 
Registered: Mar 2013
Distribution: Ubuntu
Posts: 9

Original Poster
Rep: Reputation: Disabled
Perfect colucix. I thought I needed to escape the {} characters. That's what was messing with me.
 
Old 03-03-2013, 09:16 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by masonje View Post
Perfect colucix. I thought I needed to escape the {} characters. That's what was messing with me.
You have to escape them if you use extended regular expressions (option -r) since they have a special meaning. On the other hand you don't have to escape the parentheses in this case. See the difference between the two:
Code:
sed 's/.*{\(.*\)}/\1/'
and using extended regular expressions:
Code:
sed -r 's/.*\{(.*)\}/\1/'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
Sed regular expression question kmkocot Programming 6 06-30-2010 10:29 AM
[SOLVED] sed expression help bmorency Linux - Software 4 03-17-2010 07:24 AM
sed - regular expression Vilmerok Programming 5 02-26-2009 08:44 AM
sed regular expression Ammad Linux - General 7 10-29-2008 05:52 PM


All times are GMT -5. The time now is 11:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration