|
|
#1 |
|
Hall of Famer
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,945
|
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 10.8.3; 27" iMac, OS X 10.8.3 |
|
|
|
|
|
#2 |
|
Hall of Famer
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,945
|
Solved - thanks anyway
Solved
do shell script "diskutil mount `diskutil list | awk '/ ACB-External / {print $NF}'`"
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3 |
|
|
|
|
|
#3 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 31,938
|
As an alternative to using 'awk', you could use 'grep' with the "-o" option.
E.g.: Code:
diskutil list | grep -o '[^ ]*$' [^ ] 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 |
|
|
|
|
|
#4 | |||||||||||||||||||||||
|
Hall of Famer
Join Date: Jan 2002
Posts: 3,541
|
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" |
|||||||||||||||||||||||
|
|
|
|
|
#5 |
|
League Commissioner
Join Date: Sep 2003
Location: Tokyo
Posts: 6,045
|
The final field of `diskutil list` never has spaces ("disk0s3"), so that part should be fine. The awk match can be quoted if necessary.
|
|
|
|
|
|
#6 |
|
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" |
|
|
|
|
|
#7 |
|
Hall of Famer
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,945
|
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 10.8.3; 27" iMac, OS X 10.8.3 Last edited by NovaScotian; 02-04-2007 at 07:39 AM. |
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Hall of Famer
Join Date: Jan 2002
Posts: 3,541
|
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" |
|||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|