LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2009, 07:50 AM   #1
Murdock1979
Member
 
Registered: Oct 2003
Distribution: Slackware Debian VectorLinux
Posts: 429
Blog Entries: 2

Rep: Reputation: 30
feeding grep into xargs


Hello!

I am trying to feed results from a grep into an xargs statement, but it is not working:

Code:
ls | egrep ^.\.wav$ | xargs -0 -d \n -I xxx cp "xxx" ..
The terminal gives me:

Code:
cp: cannot stat `1.wav\n3.wav\n5.wav\n7.wav\n': No such file or directory
What am I doing wrong? Any help would be great!

Murdock
 
Old 02-03-2009, 08:07 AM   #2
discomurder
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Rep: Reputation: 0
feeding grep into xargs

Hey Murdock. First, do you really need the grep? Is there some reason

ls *.wav

or some such thing wouldn't work?

Second, I don't know xargs off the top of the head well enough to know if this would help you, but you could also do something like

for file in `ls *.wav` ; do cp $file ../. ; done

Perhaps those flags to xargs are doing something else for you though beyond just passing the file name along?
 
Old 02-03-2009, 08:36 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I agree with discomurder: you don't really need egrep to create a list of *.wav files. Anyway the xargs command has too many options in your example. The following should work:
Code:
ls | egrep ^.\.wav$ | xargs -I{} cp {} /path/to/destination/dir
Two notes: 1) the -0 option is used when the input items are separated by a null string, which is not the case (unless you explicitly instruct egrep to terminate items with null, see option -Z). 2) you don't need to specify the newline character as delimiter since the shell takes care of it passing the output of egrep through the pipe: the newlines are actually removed in the passage.
 
Old 02-03-2009, 09:23 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Here's a simple command ... I know it by heart ... that I use to syntax check all of the Perl programs in a directory:

Code:
find . -name \*.pm | xargs -I{} perl -wc '{}' \;
Notice the use of the "-I{}" construct, and the subsequent use of that placeholder in the command being executed. Also notice the "\;" construct: both are essential.

Use man xargs for an explanation of this voodoo.

Tip: It could be useful to include the word echo (in front of the word perl in the above example) so that, instead of executing the commands, it simply prints (echoes...) them to the console. (The "command to be executed" becomes echo.) This will let you see exactly what is going on... exactly what commands would have been executed.

Last edited by sundialsvcs; 02-03-2009 at 09:26 AM.
 
Old 02-03-2009, 09:28 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
find . -name \*.pm | xargs -I{} perl -wc '{}' \;
Just an aside note: the \; part at the end of xargs is not necessary (and actually may trigger an error). Maybe you mixed the -exec option of find with the syntax of xargs.
 
Old 02-03-2009, 04:59 PM   #6
Murdock1979
Member
 
Registered: Oct 2003
Distribution: Slackware Debian VectorLinux
Posts: 429

Original Poster
Blog Entries: 2

Rep: Reputation: 30
Hello!

Thanks everyone for your help.

The reasons why I didn't want to just use *.wav is because I needed the flexibility of regular expressions.

Colucix: Both of your points are well taken. The -0 doesn't make sense, since the delimeter is newline. However, I still do not see why adding -d \n would actually make the statement not work.

Murdock
 
Old 02-03-2009, 07:05 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Murdock1979 View Post
However, I still do not see why adding -d \n would actually make the statement not work.
It would work if you put \n inside quotes, otherwise the shell interprets it as a literal "n" and since there is no "n" in the list of files, the whole list is passed as a single argument to xargs. You can better understand this if you do some test:
Code:
ls | egrep ^.\.wav$ | xargs -d"\n" -I{} cp {} /path/to/destination/dir   # this works
ls | egrep ^.\.wav$ | xargs -d\w -I{} cp {} /path/to/destination/dir     # this does not work
ls | egrep ^.\.wav$ | xargs -d\h -I{} cp {} /path/to/destination/dir     # this does not work
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
care and feeding of laptop batteries muddywaters General 14 01-22-2007 05:06 AM
LXer: Feeding Frenzy LXer Syndicated Linux News 0 12-30-2006 03:21 AM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 08:21 AM
squid feeding 2 subnet varun_saa Mandriva 1 02-07-2005 06:48 PM
PHP: what's wrong with this line (shell_exec, find, xargs, grep)? J_Szucs Programming 3 11-19-2003 07:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration