Operating System - HP-UX
1856437 Members
5099 Online
104113 Solutions
New Discussion

Any syntax errors in this command?

 
SOLVED
Go to solution
MAD_2
Super Advisor

Any syntax errors in this command?

I am writing a script that looks for specific owner files that are so many days old and it skips files I don't want to include in the list for each user. The syntax goes like this:

find . -type f -user $USER -mtime +90 | grep -v '^.sh$' | grep -v '^.pl$' | gre
p -v '^.sql$' >> $LOGFILE.$USER.log

However, I am not obtaining the expected result, which should exclude all files with the following pattern *.sh* *.pl* and *.sql*. Why? Where did I go wrong? Any hints?

Thanks...
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
10 REPLIES 10
Sridhar Bhaskarla
Honored Contributor

Re: Any syntax errors in this command?

Mynor,

Use "set -x" before executing the command and verify how it is resolving your pattern matching.

I would think of taking out single quotes and replacing ^ and $ with the wild char *.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
S.K. Chan
Honored Contributor

Re: Any syntax errors in this command?

Like Sri said that would work for you. You can also be specific about the "." by using ..

grep -v *\.sh*

.. for example
Rodney Hills
Honored Contributor

Re: Any syntax errors in this command?

I would remove the "^" in the grep's. They are anchoring to the beginning of line, which I don't think you want.

find . -type f -user $USER -mtime +90 | grep -v '.sh$' | grep -v '.pl$' | grep -v '.sql$' >> $LOGFILE.$USER.log

-- Rod Hills

There be dragons...
MAD_2
Super Advisor

Re: Any syntax errors in this command?

The pattern specified by Rodney works for instances such as *.sh or *.sql but not for those like *.sh.* or *.sh_* That's what I was using at first and it skips the *.sh files, which is part of what I want. However, there are also files with patterns such as *.sh.YYYYMMDD or *.sh_YYYYMMDD and I want those excluded from the search too.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Sanjay_6
Honored Contributor

Re: Any syntax errors in this command?

Hi Mynor,

Try this,

find . -type f -user $USER -mtime +90 | grep -v '\.sh$' | grep -v '\.pl$' | grep -v '\.sql$' >> $LOGFILE.$USER.log

Hope this helps.

regds
John Palmer
Honored Contributor

Re: Any syntax errors in this command?

Try this as the grep (only one is required):

grep -v -e \.sh -e \.pl -e \.sql

Regards,
John
Rodney Hills
Honored Contributor
Solution

Re: Any syntax errors in this command?

If you use extended regular expressions (available with -E option on grep), you can be more detailed on what to choose-

find ... | grep -v -E "\.[sql|pl|sh]([._][0-9]*)*" >logfile

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Any syntax errors in this command?

Oops, forgot dollar sign on end

find ... | grep -v -E "\.[sql|pl|sh]([._][0-9]*)*$" >logfile

-- Rod Hills

There be dragons...
MAD_2
Super Advisor

Re: Any syntax errors in this command?

Perfect Rodney, my question is answered... Just what I was looking for.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
James R. Ferguson
Acclaimed Contributor

Re: Any syntax errors in this command?

Hi:

Your original expression meant to match the patterns as achored at both the beginning and the ending of a line -- i.e. strings that constitute the whole line. Returning filenames from the current directory would yield strings beginning with "./" too.

Regards!

...JRF...