Grep basics

I’m trying to learn grep. But since i’m newbie in programming its hard.

I have many strings that i want to find from many log files.

Is there any generic grep string that finds string that is between x and y texts?

And if i want to find text after text x and get next 15 characters?

This page should help you get comfortable with grep
http://pegasus.rutgers.edu/~elflord/unix/grep.html

I am working on a very basic WYSIWYG HTML editor in apple script using an editable web view. I am using grep for string replacement. More specifically for searching for strings to be replaced for eventually adding html tags like

and

to the selected string. The link above was beyond valuable to me.

-sD-
Dr. Scotty Delicious, Scientist.

This book should be under your pillow at night:

http://www.oreilly.com/catalog/regex/

With a word of warning, cirno: most of the examples are in perl scripts.

Might not help you specifically but might help others:

Both BBEdit and MailSmith (both by Barebones Software) have some very straightforward instuctions for using GREP syntax (i.e. how to make a search string), since both support it for searches and filtering.

BBEdit guide is great.

I try to get text between “offset” and “sec”, but this string has invisible “¿” between every character:

“2 Mar 11:21:26 ntpdate[16462]: adjust time server 123.45.67.89 offset 0.021 sec”

I didnt find a way to search just strings. Does grep search only strings in files?

set testthis to "offset testing sec"
do shell script "grep offset.*?sec " & testthis

Try this:

set testthis to "offset testing sec"
do shell script "echo " & quoted form of testthis & " | /usr/bin/grep 'offset.*\\?sec'"

Just a heads up they are up to the third addition now - http://www.oreilly.com/catalog/regex3/

For some reason, it returns “offset testing sec”???

Hi cirno,

grep returns whole lines. You need sed:

http://www.grymoire.com/Unix/

or AppleScript.

gl,

I’m thinking awk might be better? You can ajust the delimiters by using the -F option. ie: if there are tabs betwen the fields you would say “awk -F \t …”

echo "2 Mar 11:21:26 ntpdate[16462]: adjust time server 123.45.67.89 offset 0.021 sec" | awk '{print $10}'

Try this instead:

set test to "2 Mar 11:21:26 ntpdate[16462]: adjust time server 123.45.67.89 offset 0.021 sec"

do shell script "echo " & quoted form of test & " | /usr/bin/grep -o 'offset.*\\?sec'"
set someVar to text 7 thru -5 of result