Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find | xargs | awk line not working 3

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

The below line works great on the server;

find ./ -ctime 0 -name 'DBload.*.log' | xargs ls -l | awk '$5>32000 {print $0}'

However, I've tried running this as part of a larger script from a central host, like this;

ssh -n servername "find /path/to/check -ctime 0 -name 'DBload.*.log' | xargs ls -l | awk '$5>32000 {print $0}'"

and I get awk syntax errors. If I drop the awk part it works, but then I have to use another statement to rid myself of files with sizes below 32000.

All help appreciated.
Alan


 
Try escaping the single quotes i.e.
Code:
ssh -n servername "find /path/to/check -ctime 0 -name \'DBload.*.log\' | xargs ls -l | awk \'$5>32000 {print $0}\'"
With out the escapes the singel quotes are interpreted on the local host. With the escapes they should be interpreted on the remote host.

BTW - it may be worth checking whether your find has a '-l' (that's an ell) option to remove the need for the xargs call.

On the internet no one knows you're a dog

Columb Healy
 
Hi Columb,

Well I'm afraid that even escaping the singles didn't work, but am very pleased to have found out about the -ls expression for find. Two years doing this job and still learning new stuff.

The awk part is odd. I have used similar constructs in the past using sed and singles without any problem.

BTW i'm using solaris 10 but will be ssh'ing to solaris 8 and 9 boxes also.

Thanks for your help.
Alan
 
Why not simply this ?
ssh -n servername "find /path/to/check -ctime 0 -name 'DBload.*.log' -size +64"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Wonderful. Thanks PHV.

Note to self..."read all available expressions and switches of commands before jumping straight into awk".

Much appreciated. Just for my guide...do you have any idea why the awk part wasn't working remotely?

Thanks
Alan
 
I've played around with this and I'm getting the same rather strange results as you. I got this UUOC example of what you're doing to work
Code:
ssh otherhost 'cat /etc/passwd | awk "/root/ {print}"'
which has the quotes the 'wrong' way round.

At this point I get a bit lost. I'm sure one of the super gurus (PH?) can explain.

On the internet no one knows you're a dog

Columb Healy
 
Thanks for the perseverance Columb.

I have used a similar technique in the past with sed, like this;

res="$( ssh -n $h "/bin/test -d $core2 && find $core2 -mtime -1 | sed 's:/users/corefiles::g';/bin/test -d $core1 && find $core1 -mtime -1 | sed 's:/users/core::g'" )"

which works just fine, so I'm guessing it's an awk "feature".

Glad to have learnt a little more about find today though.
Only issue being that I was trying to be smart(ish) with awk ;-).

Alan
 
Columnb, a good way to check what will happen is to change the ssh command to an echo, you'll see then why it didn't work:

Code:
$ echo "find /path/to/check -ctime 0 -name \'DBload.*.log\' | xargs ls -l | awk \'$5>32000 {print $0}\'"
find /path/to/check -ctime 0 -name \'DBload.*.log\' | xargs ls -l | awk \'>32000 {print ksh}\'
$

As you can see the $5 in the awk script has been gobbled up and $0 has been replaced by my shell name. The shell will evaluate all variables between "...", it doesn't care that you have protected them with single-quotes because it treats them simply as part of the string once they are surrounded by double quotes, they no longer have special meaning. Therefore you don't even need to escape them. This should have the expected result:

Code:
$ echo "find /path/to/check -ctime 0 -name 'DBload.*.log' | xargs ls -l | awk '[COLOR=red]\[/color]$5>32000 {print [COLOR=red]\[/color]$0}'"
find /path/to/check -ctime 0 -name 'DBload.*.log' | xargs ls -l | awk '$5>32000 {print $0}'
$


Annihilannic.
 
Thanks Annnihilannic, for the explanation and the idea to check with echo.

Much obliged to all.
Alan
 
Annihilannic
Good tip about using echo to see how command lines are resolved - one of those 'Duh - why didn't I think of that' - now part of my toolkit.

That's another beer I owe you!


On the internet no one knows you're a dog

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top