Use unix grep command in AppleScript

I am trying to use grep to select records by search item but need to do this by a selected column.

The code I have works but have problems if the search item appears in another column.

Is it possible to specify the column to be searched.

Any guidance would be gratefully received.


set searchItem to 2018 as integer -- extract year

set foundRecs to do shell script "grep " & searchItem & space & filePath -- only read matching records

iMac (Retina 5K, 27-inch, 2017), macOS High Sierra (10.13.5)

Posted on Jul 16, 2018 9:36 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 17, 2018 6:00 AM

This is tricky to do with grep since it involves some pretty heavy-duty regular expression formatting to filter specific fields in your input value.


However, there are alternatives that may be easier. For example, awk makes it pretty easy to match on specific fields in the input:


do shell script "awk '{if(match($2," & searchItem & ")) print $0}' " & filePath

This prints out all lines where the second field ($2) matches the searchItem string.


If your input data is comma-delimited you may need to add a '-F,' switch to tell it to use commas as the field delimiter (as opposed to whitespace).

7 replies
Question marked as Top-ranking reply

Jul 17, 2018 6:00 AM in response to Frinton Fogey

This is tricky to do with grep since it involves some pretty heavy-duty regular expression formatting to filter specific fields in your input value.


However, there are alternatives that may be easier. For example, awk makes it pretty easy to match on specific fields in the input:


do shell script "awk '{if(match($2," & searchItem & ")) print $0}' " & filePath

This prints out all lines where the second field ($2) matches the searchItem string.


If your input data is comma-delimited you may need to add a '-F,' switch to tell it to use commas as the field delimiter (as opposed to whitespace).

Jul 17, 2018 5:28 AM in response to VikingOSX

Many thanks for your reply and I apologise for not including all the relevant information, it was a tentative enquiry to see what possibilities were available. You were correct, I want to retrieve only matching records from a large csv file, however, when I use your amended code I appear to retrieve all the records, not the matching ones. I have obviously missed something and, as you can probably tell, my knowledge of Unix commands is somewhat limited!!


set filePath to quoted form of "/Users/Ours/Documents/Cats Protection/Membership/CP Members copy.csv"

set searchItem to "Joan"

set theCol to 4

set foundRecs to do shell script "awk -F, '(index($" & theCol & "," & searchItem & ") != 0)' " & filePath

Jul 16, 2018 12:07 PM in response to Frinton Fogey

Show some sample columnar data from your filePath file. If this is a CSV-like formatted file what is the separator character?


The grep command does not take an integer, it takes text numeric search string. There are two different formats of your command depending on whether 1) you want to find 2018 in the filePath, or 2) in the actual filePath contents.


It sounds to me that you want something else, and haven't provided all the necessary information for the solution.


1) in the filePath name

set searchItem to "2018"

set foundRecs to do shell script "grep " & searchItem & " <<<" & filePath


2) in the filePath contents

set searchItem to "2018"

set foundRecs to do shell script "grep " & searchItem & space & filePath

Jul 16, 2018 1:41 PM in response to VikingOSX

And here is how you use Awk to print all lines containing 2018 in column 1 of a comma separated values file. The simplified CSV is:


This,That,2018

2018,That,2016

2016,This,2018

2018,2018,2018


and the code is:


set filePath to POSIX path of ((path to desktop as text) & "foo.csv") as text


set theCol to 1

set amatch to "2018"


-- columns in awk can be specified as $1, $2, ... $NF (last column). The field separator (-F) tells awk it is a comma.

-- UNIX command line: awk -F, '(index($1, "2018") != 0)' filePath

do shell script "awk -F, '(index($" & theCol & "," & amatch & ") != 0)' " & filePath

return

Result:

"2018,That,2016

2018,2018,2018"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Use unix grep command in AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.