Simple string searching in a text file

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Simple string searching in a text file Tue Aug 07, 2012 2:53 am • by DBCooper
I'm sure this is dead simple, but I haven't found a good example yet. I have an XML file that is created by a Garmin GPS watch that I wear when I'm running or biking. I'd like to automate the process of archiving these files by workout type whenever they are downloaded from my watch.

Here's what the top part of one of these .TCX files looks like:

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">

  <Activities>
    <Activity Sport="Biking">
      <Id>2012-08-04T21:03:31Z</Id>
      .
      .
      .
      many more lines of XML text


Notice the
Code: Select all
<Activity Sport="Biking">
line which indicates the workout type.

These .TCX files are not indexed by Spotlight, so I can't use a simple "Contents contains…" condition. It seems like a simple embedded script would be just right here.

What would that script look like?

To sum up, I have two conditions...
1. Extension is TCX
2. File contains the text "Biking"

It's #2 that's giving me grief.

Any suggestions?
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 8:26 am • by flynn
What happens if you change the extension to txt?
Maybe you could copy the file, change the extension to txt, find the string, sort accordingly then change the extension back to TCX.
flynn
 
Posts: 66
Joined: Sun Apr 08, 2012 12:41 pm
Location: Philippines

Re: Simple string searching in a text file Tue Aug 07, 2012 1:24 pm • by a_freyer
Use this shell script to match (return 0) only if the file contains the case-insensitive string biking once:

Code: Select all
[ $(grep -ci biking $1) -gt 0 ]


To be clear, copy this line to a passes shell script condition.

No need to change the file extension.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 4:01 pm • by DBCooper
That's an interesting approach, and it makes perfect sense. Sadly, it doesn't work for me. It produces the desired result when I run that grep from the terminal

Code: Select all
$ grep -ci biking 2012-08-05-144902.TCX
1

Is the exit status not being set correctly?
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 4:06 pm • by a_freyer
DBCooper wrote:That's an interesting approach, and it makes perfect sense. Sadly, it doesn't work for me. It produces the desired result when I run that grep from the terminal

Code: Select all
$ grep -ci biking 2012-08-05-144902.TCX
1

Is the exit status not being set correctly?



Silly me - I didn't include the exit status: EDIT - actually, the following is redundant.

Code: Select all
exit $([ $(grep -ci biking $1) -gt 0 ])
Last edited by a_freyer on Tue Aug 07, 2012 5:07 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 4:20 pm • by DBCooper
Ah, that makes sense. But wouldn't that set the exit status to be non-zero in the event that the text is found?

I tested your newest suggestion and it too fails to trigger. I know the rest of the rule is working.
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 4:38 pm • by a_freyer
DBCooper wrote:Ah, that makes sense. But wouldn't that set the exit status to be non-zero in the event that the text is found?

I tested your newest suggestion and it too fails to trigger. I know the rest of the rule is working.


This should work just fine. I've confirmed with a few test files.Have you tried this rule addition as a separate rule?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 4:55 pm • by DBCooper
Here's my setup with just this one rule in place. It's still not working.

Image

Anything look fishy?
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 5:06 pm • by a_freyer
Ack. Im debugging this from my iPad now, so I can't test.

Either of these should be working just fine. When the greater than if statement matches, it will return an exit status of zero. When it does not match, it will return an exit status of zero. Therefore the exit statement is redundant.

Either of these should be working. Are you sure that the file you're testing on has biking within it?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 5:08 pm • by DBCooper
PM sent.
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 5:09 pm • by a_freyer
DBCooper wrote:That's an interesting approach, and it makes perfect sense. Sadly, it doesn't work for me. It produces the desired result when I run that grep from the terminal

Code: Select all
$ grep -ci biking 2012-08-05-144902.TCX
1

Is the exit status not being set correctly?



If you run the bracketed statement in terminal, and then run an echo $? to retrieve the exit status. What does terminal return for you?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 5:17 pm • by DBCooper
Which bracketed statement?
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Re: Simple string searching in a text file Tue Aug 07, 2012 5:18 pm • by a_freyer
hmmmm... this is working for me.

Image

Image
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 5:18 pm • by a_freyer
DBCooper wrote:Which bracketed statement?



Code: Select all
[ $(grep -ci biking $1) -gt 0 ]
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Simple string searching in a text file Tue Aug 07, 2012 5:23 pm • by DBCooper
Like this?

Code: Select all
$ [ $(grep -ci biking $1) -gt 0 ]

^C
$ echo $?
1
$ echo $?
0

I'm not sure how the $1 plays in that.
DBCooper
 
Posts: 20
Joined: Tue Aug 07, 2012 2:37 am

Next

Return to Support