• 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: Finding lines containing foo in a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding lines containing foo in a file


  • Subject: Re: Finding lines containing foo in a file
  • From: Deivy Petrescu <email@hidden>
  • Date: Thu, 22 Dec 2005 17:00:35 -0500


On Dec 21, 2005, at 6:08 PM, Stockly, Ed wrote:
Not safe. One more reason to prefer pure AppleScript to shell scripting.

Thanks to help from this list, I got the shell script in a workable form

set itemNumber to "25307"
set filepath to quoted form of POSIX path of (choose file)
set myShellScript to ("grep " & (itemNumber) & " " & filepath) as text
set foundLines to do shell script myShellScript

I getting the desired result almost instantly.

The problem is if the target file is on the top level of the startup volume or on the network, it still doesn't work, giving me either "type 1 error" or "input/output error". (This script is meant for users off-site who, as mac users do, put files wherever they damn well feel like it, so unless this!
can be fixed we can't use it.)


This really shouldn't be thi!
 s diffic
ult.

Ed

Ed, I forgot to mention that my previous script was tested and worked.
However, all the follow up posters had excellent points, so I incorporated all of them in this next script.
It works both with files at the root level or with files on mounted network volumes.
I tested !


I believe that this is faster than an AS solution without do shell script. One point that was not mentioned earlier, this is great for ascii text, but if you have a formated file, say word or rtf, then, using the application should be better because you avoid all the formatting garbage.

<script>

set itemstofind to {"egrep", "fgrep", "shell", "GNU"}
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "|"}
set {itemstofind, AppleScript's text item delimiters} to {quoted form of (itemstofind as string), astid}
set filepath to quoted form of POSIX path of ((choose file) as Unicode text)


my GetLinesFromFile(itemstofind, filepath, false, false)


on GetLinesFromFile(itemstofind, filepath, Ireverse, linenumber)
set gr to "egrep "
if Ireverse then set gr to gr & "-v " -- get lines *not* containing the pattern
if linenumber then set gr to gr & "-n " -- get line numbers
return (do shell script "tr '\\r' '\\n' < " & filepath & " |" & gr & itemstofind)
end GetLinesFromFile


</script>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Finding lines containing foo in a file (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: Simple Repeat until loop problem
  • Next by Date: Re: How do I test if app is running / file is open?
  • Previous by thread: Re: Finding lines containing foo in a file
  • Next by thread: Re: Finding lines containing foo in a file
  • Index(es):
    • Date
    • Thread