in applescript editor:
do shell script "grep -w 'SomeText' /tmp/test"
ignores -w
in Bash:
grep -w 'SomeText' /tmp/test
not ignores arguments
But for example arguments -v (negative) works in AppleScript with do shell script
it is happening on both different computers with different systems
how i can use -w argument in grep from applescript?
Thanks!
Regardless of where I run the grep -w ...
command from, Terminal or ApplesScript's do shell script
command, I get identical output .
The manual page for the -w
option in grep
states the following:
−w, −−word-regexp
The expression is searched for as a word (as if surrounded by '[[:<:]]' and '[[:>:]]'; see
re_format(7)).
The manual page for re_format
states:
There are two special cases‡ of bracket expressions: the bracket expressions [[:<:]] and [[:>:]] match the null string at the beginning and end of a word respectively. A word is defined as a sequence of word characters which is neither preceded nor followed by word characters. A word character is an alnum character (as defined by ctype(3)) or an underscore.
Contents of /tmp/test
:
$ cat /tmp/test SomeText MoreText ASomeTextZ Other Text 0 SomeText 1 $
Using grep
without -w
on /tmp/test
:
$ grep 'SomeText' /tmp/test SomeText ASomeTextZ 0 SomeText 1 $
grep
finds all three lines containing 'SomeText'
. Using grep
with -w
on /tmp/test
:
$ grep -w 'SomeText' /tmp/test SomeText 0 SomeText 1 $
grep -w
finds only the lines conforming to what's stated in the manual page excerpts shown above. In this case, only two of the three lines that contain 'SomeText'
.
The output of each grep
command , show above, when wrapped in a do shell script
command in AppleScript are identical, as should be.
Because these are the expected results is why I'm adamant about following How to create a Minimal, Complete,and Verifiable example , when asking questions such that you have, in the manner you have!
I'd suggest you show us the actual content of your /tmp/test
file and the actual output you get from each of the grep
commands , with and without the -w
option , from both Terminal and AppleScript's do shell script
command .
Although it shouldn't make a difference, nonetheless you should also provide macOS version info so we can test this under the actual version of macOS you're using, so as to see if that's a relevant factor in the equation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.