• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Regex on OS X
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Regex on OS X
      • From: "Dennis W. Manasco" <email@hidden>
    • Re: Regex on OS X
      • From: John Delacour <email@hidden>
References: 
 >Re: Regex on OS X (From: John Delacour <email@hidden>)

  • Prev by Date: Eudora 5.2b10 release notes[OT]
  • Next by Date: RE: Unmount remote volume (10.2.1)
  • Previous by thread: Re: Regex on OS X
  • Next by thread: Re: Regex on OS X
  • Index(es):
    • Date
    • Thread