Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 02-03-2007, 02:17 PM   #1
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,103
Regex for "Last Word" of line

I know that a line from diskutil list contains "ACB-External", say, and grep will find that line of the text. What I want is the last word of that line - the node address - something like "disk1s3".

In AppleScript, this works, but uses two do shell script constructions to do it:

do shell script "diskutil mount " & last word of (do shell script "diskutil list | grep ACB-External")

Rather crude. The question: how can I form a regex to find that last word given the name of the disk involved (I want to mount it with diskutil mount), so I can combine those instructions?
__________________
17" MBP, OS X; 27" iMac, both OS X 10.9.3
NovaScotian is offline   Reply With Quote
Old 02-03-2007, 05:30 PM   #2
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,103
Solved - thanks anyway

Solved

do shell script "diskutil mount `diskutil list | awk '/ ACB-External / {print $NF}'`"
__________________
17" MBP, OS X; 27" iMac, both OS X 10.9.3
NovaScotian is offline   Reply With Quote
Old 02-03-2007, 09:10 PM   #3
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,388
As an alternative to using 'awk', you could use 'grep' with the "-o" option.
E.g.:

Code:
diskutil list | grep -o '[^ ]*$'
Explanation of regex:
[^ ] matches any non-space character
[^ ]* matches zero or more non-space characters
The $ anchors it to the end of the line - so it matches zero or more non-space characters at the end of the line.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 02-04-2007, 02:47 AM   #4
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by NovaScotian
Solved

do shell script "diskutil mount `diskutil list | awk '/ ACB-External / {print $NF}'`"

Won't this break if your drive's name has a space in it?
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 02-04-2007, 03:33 AM   #5
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,299
The final field of `diskutil list` never has spaces ("disk0s3"), so that part should be fine. The awk match can be quoted if necessary.
acme.mail.order is offline   Reply With Quote
Old 02-04-2007, 04:16 AM   #6
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Ah, yeah, I was thinking of /bin/df.
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 02-04-2007, 07:33 AM   #7
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,103
Thank you, gentlemen; I don't know why RegEx continues to remain so mysterious to me - could be senility, I suppose.

Hayne's form returns all the devices - how do I build in the named one I want, or is this the way:

diskutil list | grep ACB-External | grep -o '[^ ]*$'

which works, but I'm calling grep twice.
__________________
17" MBP, OS X; 27" iMac, both OS X 10.9.3

Last edited by NovaScotian; 02-04-2007 at 07:39 AM.
NovaScotian is offline   Reply With Quote
Old 02-04-2007, 12:03 PM   #8
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by NovaScotian
Thank you, gentlemen; I don't know why RegEx continues to remain so mysterious to me - could be senility, I suppose.

http://www.amazon.com/Mastering-Regu...dp/0596528124/

Best book on the subject, if you're really curious.
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Forum Jump



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


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.