1

I'd need to filter through a list files (output of top command) for some info:

  • Firstly I need to list files with a find
  • Then, I need to grep for lines with a pattern
  • Finally, I need to pickup one column I'm interested into.

Seems I'm unable to bind the find with both grep and awk:

My first attempt:

find . -name "high-cpu.out" -exec grep "jboss" | awk '{ print $5 }' {} \;

find: missing argument to `-exec'
awk: fatal: cannot open file `{}' for reading (No such file or directory)

Second attempt:

find . -name "high-cpu.out" -exec grep "jboss" {} \ | awk '{ print $5 }';

find: missing argument to `-exec'

Any help? Thanks

2 Answers 2

5

I recommend using AWK to match the pattern instead of grep:

find . -name "high-cpu.out" -exec awk '/jboss/ { print $5 }' {} +

This also allows the use of the + variant of -exec, resulting in fewer awk invocations.

When you tried

find . -name "high-cpu.out" -exec grep "jboss" | awk '{ print $5 }' {} \;

the shell processed the pipe first, not find. That led to find failing with “missing argument to ‘-exec’”: {} \; wasn’t included in the arguments passed to it (they were passed to awk).

Your second attempt failed because find couldn’t see the closing semi-colon since it wasn’t separated from the awk argument. In any case the pipe wouldn’t have worked, since find doesn’t delegate to the shell.

0
0

find . -name "high-cpu.out" -exec grep "jboss" | awk '{ print $5 }' {} ;

find: missing argument to -exec' awk: fatal: cannot open file {}' for reading (No such file or directory)

This cannot work until the exec command is broken/finished at the "|" level so {} cannot be interpreted by the awk sub-command.

so you have to do

.......  -exec awk '/pattern/ {operation}' {} +

or

 find . -name "high-cpu.out" -exec grep pattern {} \; | awk '{operation}'

but it is never a good idea to use many commands sed+awk+grep for the same work ; You have to choose one of them & adapt the regexp on pattern to match your needs

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .