How to run multiple piped commands in a find -exec statement?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to run multiple piped commands in a find -exec statement?
# 1  
Old 03-02-2011
How to run multiple piped commands in a find -exec statement?

I can't get this to work. Running a single command works fine:
Code:
find . -name "*.dat" -exec wc -l '{}' \;

gives me the file name and number of lines in each .dat file in the directory.

But what if I want to pipe commands, e.g. to grep something and get the number of lines with that pattern in each file?

These both give me syntax errors:
Code:
find . -name "*.dat" -exec grep XXX | wc -l '{}' \;
find . -name "*.dat" -exec grep XXX '{}' | wc -l \;

I tried quoting the whole piped expression both ways as well:
Code:
find . -name "*.dat" -exec "grep XXX | wc -l '{}'" \;
find . -name "*.dat" -exec "grep XXX '{}' | wc -l" \;

These both run without errors but neither produces any output (and there are XXX patterns in the files).

How do I write this command?

Thanks.

Last edited by Franklin52; 03-02-2011 at 10:19 AM.. Reason: Please use code tags
# 2  
Old 03-02-2011
Your problem arises from the fact that "find" is not a shell and does not know how to process the pipeline character '|'. My solution would be to switch to a shell. My login shell is ksh so I can do:
Code:
find . -name '*.dat'  | while read filename ; do grep XXX $filename | wc -l ; done

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 03-02-2011
try to use xargs -

find . -name '*.dat' | xargs wc -l
# 4  
Old 08-02-2011
Find with Pipe

Try this

find . -type d -name somedirname -exec ksh -c 'echo -n $1" ";ls -ltr $1|wc -l' {} {} \;


This will serach for all directories from current direcotry and will give count of number (+1) of files/dirs present in that directory.

For your original question:
find . -type f -name "*.dat" -exec ksh -c 'echo -n $1" ";grep XXXX $1|wc -l ' {} {} \;
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run 2 exec commands

I have to create two instances of jBoss 5.1.0 GA. In order to do that I have to execute the following in start-jboss.sh: find . -exec /opt/novell/idm/jboss/bin/run.sh -Djboss.service.binding.set=ports-01 -c IDMProv -b 0.0.0.0 \; -exec /opt/novell/idm/jboss/bin/run.sh... (4 Replies)
Discussion started by: Joydeep Ghosh
4 Replies

2. Shell Programming and Scripting

Run multiple commands in ssh

Hi All, I have the below requirement. I want to copy the local file to remote after that i need to run the local script on a remote machine.When i use two ssh commnds i can achieve this. But i want to achieve this using one ssh command. Below command to copy the local file to remote ssh -q... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

3. UNIX for Dummies Questions & Answers

Using find -exec with multiple commands :(-

Hi all, Am wanting to do a ls -l of the files and do a cat of it at the same time, ideally, I am hoping that the following work but obvisouly it is not working to what I am wanting it to ... hu hu hu :wall: find . -name "BACKUP_TIMESTAMP.log" -exec "ls -l basename {} ; cat {}" \; ... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. UNIX for Dummies Questions & Answers

Run multiple commands

Hi All, Is it possible to run second/multiple commands at a time in script before the completion/return of first command? Pls reply. (5 Replies)
Discussion started by: cns1710
5 Replies

5. SuSE

Allow multiple users to run several root commands

I am using SUSE Linux Enterprise Server 10 SP2 (i586) and I had earlier ammended my sudoers file to allow users to become root user with "sudo su - " command Now I am trying to add multiple users to the sudoers file to run several commands such as restarting the server, restarting the nagios... (9 Replies)
Discussion started by: hedkandi
9 Replies

6. UNIX for Advanced & Expert Users

find -exec with 2 commands doesn't work (error incomplete staement)

Hi Gurues, I need to modify an existing script that uses find to search a folder, and then move its contents to a folder. What I need to do is run gzip on each file after it's moved. So, I ran this little test: Put a ls.tar file on my $HOME, mkdir tmp, and then: virtuo@tnpmprd01: find .... (3 Replies)
Discussion started by: llagos
3 Replies

7. Shell Programming and Scripting

find command to use multiple -exec options

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside exec. How do I... (1 Reply)
Discussion started by: prasanna1157
1 Replies

8. UNIX for Dummies Questions & Answers

How to run two commands from a exec call in a c program

Hi, I have to run two commands one after another from a c program. How can i do this with exec system calls. i tried giving them as argument to execv but it is not working.please help thanks (3 Replies)
Discussion started by: suryashikha
3 Replies

9. Shell Programming and Scripting

How to execute piped command using exec or system

Hi All, I want to execute a piped command like 'ls /opt | grep xml' using array as parameters list. How can I do that? (2 Replies)
Discussion started by: bharadiaam
2 Replies

10. UNIX for Dummies Questions & Answers

Command to run multiple commands from a file.

I need a command, which could run mutliple commands from a file. Let's say, I have mv fileA1 fileB1 mv fileA2 fileB2 ..... mv fileA20 fileB20 I put these commands in a file, then I need a command to run the file as a whole so that I don't need to type 20 times... Anyone tell me how to... (8 Replies)
Discussion started by: kaixinsjtu
8 Replies
Login or Register to Ask a Question