Operating System - HP-UX
1849868 Members
1632 Online
104045 Solutions
New Discussion

find comand - executing >1 statement

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

find comand - executing >1 statement

Hi,

I'm attempting to execute >1 statement in within the -exec option of the find command:

bash-2.03$ find . -depth -type f -name "*.xml" -exec grep -l "summaryid" |awk -F/ '{print("cp" $0 "/tmp/" $NF)}' {} \;
find: incomplete statement
awk: can't open {}

From the command line above, I'm attempting to list the instances of files containing the string "simmaryid" within the current directory, and copy those files to another directory if the "grep" command succeeds. However, I obtained the error message shown above.

I had to resort to writting it in a longer method, which eventually works:
#!/bin/sh

find . -type f -name ".xml" -print |while read i
do
if [! `grep -l summaryid`]; then
echo "PATTERN DNE"
else
cp $i /tmp
fi
done

I was wondering if anyone could show me the method of executing >1 statement within the -exec option of the find command? I'm trying to do it within 1 line.

Thanks in advance.
Danny
8 REPLIES 8
Ermin Borovac
Honored Contributor

Re: find comand - executing >1 statement

How about

find . -depth -type f -name "*.xml" -exec grep -l "summaryid" {} \; |awk -F/ '{print("cp" $0 "/tmp/" $NF)}'
Stephen Keane
Honored Contributor

Re: find comand - executing >1 statement

Write a shell script containing all the lines you want. Call the shell script from the exec in the find comand.
Pete Randall
Outstanding Contributor

Re: find comand - executing >1 statement

Danny,

Try using xargs instead:

$ find . -depth -type f -name "*.xml" | xargs grep -l "summaryid" |awk -F/ '{print("cp" $0 "/tmp/" $NF)}'


Pete

Pete
Danny Fang
Frequent Advisor

Re: find comand - executing >1 statement

Hi everyone,

I was wondering if there's another way of doing the same thing shown below, still the one-liner of the -exec option; but without using awk ?

Greatly appreciate any ideas.

Danny
Kent Ostby
Honored Contributor

Re: find comand - executing >1 statement

Well this still uses awk, but it encapsulates it into a file so its cleaner.

Create useme.awk as:

{print"cp", $0, "/tmp/" $NF}

Script file:
rm ./useit

find . -depth -type f -name "*.xml" | xargs grep -l "summaryid" |awk -f useme.awk > useit

chmod +x useit
./useit

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Ermin Borovac
Honored Contributor
Solution

Re: find comand - executing >1 statement

You can do it with multiple exec statements.

$ find . -depth -type f -name "*.xml" -exec grep -l "summaryid" {} \; -exec cp {} /tmp \;

Second set of curly braces ({}) will only have files that passed the grep.
Ermin Borovac
Honored Contributor

Re: find comand - executing >1 statement

You may want to replace 'grep -l' with 'grep -q' in my last post.
Sandman!
Honored Contributor

Re: find comand - executing >1 statement

You could use the following one liner w/o awk

# find . -depth -type f -name "*.xml" -exec grep -il "summaryid" {} \; | xargs -i cp {} /tmp/{}