Re: Regex on OS X
Re: Regex on OS X
- Subject: Re: Regex on OS X
- From: bill fancher <email@hidden>
- Date: Thu, 3 Oct 2002 08:34:03 -0700
On Wednesday, October 2, 2002, at 03:25 AM, John Delacour wrote:
I notice you have not actually supplied the UNIX shell script that
_does_ print all the lines whose name is 'Jan' and not 'Jan*' without
the preceding number and space. I look forward to seeing this.
AppleScript is quite capable of doing this unaided, but, for your
amusement and derision:
set s to "
1 Jan Smith
2 Jane Smith
3 Jan Jones
4 Jane King
5 Jan Long
6 Jan Mann
7 Janet Hale
8 Janine Winter
9 Moose
"
set cmd to "echo '" & s & "'|awk '/Jan[ \\t]+/ {sub(/[0-9]*[
\\t]+/,\"\");print}'"
set text item delimiters to (ASCII character 10) -- to convert CR to LF
do shell script paragraphs of cmd as text -- convert CR to LF and run
script
For the non-unix-insane, awk reads input lines and matches them against
patterns. If the line matches the pattern, awk performs actions. The
command line is basically:
awk 'pattern1 {action11;action12;...}; pattern2 {action21;...};...'
Anyone interested in documentation for awk can get it by running
do shell script "man awk|col -b"
The point is that grep is one limited implementation of regular
expressions and people often confuse "grep" and "regex"
I find grep's shortcoming is not so much its regular expression
implementation (which is quite standard), as its limited behavior once
a pattern is matched. If you want to do much more than print the line
or print a match count, you probably don't want grep. Still, I
frequently find it useful.
--
bill
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.