views:

64

answers:

1

I just thought I had found my solution because the command works in my test directory.

grep -H -e 'author="[^"].*' *.xml | cut -d: -f1 | xargs -I '{}' mv {} mydir/.

But using the command in the non-test-direcory the command did not work: This is the error message:

grep: unknown option -- O
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

Not even this worked:

$ grep -H author *.xml

or this:

$ grep -H 'author' *.xml

(same error message)

I suspect it has some relation to the file names or the amount of files. I have almost 3000 files in the non-test-directory and only 20 in my test directory. In both directories almost all file names contain spaces and " - ".

Some more info:

  • I'm using Cygwin.
  • I am not allowed to change the filenames
+1  A: 

Try this (updated):

grep -HlZ 'author="[^"].*' -- *.xml | xargs -0 -I {} mv -- {} mydir/

EXPLANATION (updated)

  • In your "real" directory you have a file with name starting with -O. Your shell expands the file list *.xml and grep takes your - starting filename as an option (not valid). Same thing happens with mv. As explained in the Common options section of info coreutils, you can use -- to delimit the option list. What comes after -- is considered as an operand, not an option.

  • Using the -l (lowercase L) option, grep outputs only the filename of matching files, so you don't need to use cut.

  • To correctly handle every strange filename, you have to use the pair -Z in grep and -0 in xargs.

  • No need to use -e because your pattern does not begin with -.

Hope this will help!

Vanni Totaro
Thanks a lot for the good advice!A part of the problem is at least solved now.God news: grep does not complain and it finds one file (with this horrible name):!! Importing device - to be finished later.xmlBad news: grep does not list all files containing the searched string - for example these files beginning with single dash, duouble dash and triple underscore:-OFF.xml--operation.xml___Checklist - menu.xmlShould I maybe do something with the variable IFS?/ T
Tony
@Tony: I think `grep` lists those files, the problem is `mv`. You must do the `--` trick with `mv` too. Now I'll edit the answer to fix the issue. If it works please accept my answer! :)
Vanni Totaro
Yes it worked! Good!/ T
Tony
@Tony: great you liked my answer... please remember to set my answer as accepted! :)
Vanni Totaro