rm files not matching (tricky) !!
rm files not matching (tricky) !!
am 09.09.2007 05:24:38 von onkar
I have large number of files in a directory with filenames
containing
(1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
might be 1232344saasderv while others might have name awer345345ds
and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
and
(2) some files containing only digits [0-9] .
I want to write a script removing only those files that contain both
alphabets and digits. One trick might me to use only property (2) to
remove files with property (1). (this is like grep -v grep )
Please help me /
Re: rm files not matching (tricky) !!
am 09.09.2007 06:03:53 von cfajohnson
On 2007-09-09, onkar wrote:
> I have large number of files in a directory with filenames
> containing
>
> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> might be 1232344saasderv while others might have name awer345345ds
> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>
> and
>
> (2) some files containing only digits [0-9] .
>
>
> I want to write a script removing only those files that contain both
> alphabets and digits.
rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: rm files not matching (tricky) !!
am 09.09.2007 06:26:55 von onkar
On Sep 9, 9:03 am, "Chris F.A. Johnson" wrote:
> On 2007-09-09, onkar wrote:
> > I have large number of files in a directory with filenames
> > containing
>
> > (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> > might be 1232344saasderv while others might have name awer345345ds
> > and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>
> > and
>
> > (2) some files containing only digits [0-9] .
>
> > I want to write a script removing only those files that contain both
> > alphabets and digits.
>
> rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*
>
> --
> Chris F.A. Johnson, author
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence
ok !! thats correct but I was interested in knowing how to invert. I
mean how to use property (2) that all-digit filename can be used to
remove the files whose filenames contain both digits and alphabets. I
woiuld be greatful if you can help me with that ..
Re: rm files not matching (tricky) !!
am 09.09.2007 12:04:13 von Michal Nazarewicz
>> On 2007-09-09, onkar wrote:
>>> I have large number of files in a directory with filenames
>>> containing (1) numbers [0-9] and alphabets [a-z] (for e.g., one file
>>> name might be 1232344saasderv while others might have name
>>> awer345345ds and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>>> and (2) some files containing only digits [0-9] .
>>>
>>> I want to write a script removing only those files that contain both
>>> alphabets and digits.
> On Sep 9, 9:03 am, "Chris F.A. Johnson" wrote:
>> rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*
rm -f -- *[a-z]*[0-9]* *[0-9]*[a-z]*
onkar writes:
> ok !! thats correct but I was interested in knowing how to invert. I
> mean how to use property (2) that all-digit filename can be used to
> remove the files whose filenames contain both digits and alphabets. I
> woiuld be greatful if you can help me with that ..
As far as I understand your question you cannot do such a thing. As far
as I know you cannot test if file name contains only digits -- you can
only test if file does not contain any other character, ie. you can
create set of files which do not have property (2) but to create set of
files which have property (2) you'd need to iterate through all files
and exclude those which do not have property (2).
To remove all files without property 2:
rm -f -- *[!0-9]*
But to remove all files with property 2:
for file in *; do
case $file in (*[!0-9]*) continue ; esac
rm -f -- "$file"
done
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--
Re: rm files not matching (tricky) !!
am 09.09.2007 12:31:09 von Stephane CHAZELAS
2007-09-09, 12:04(+02), Michal Nazarewicz:
[...]
> As far as I understand your question you cannot do such a thing. As far
> as I know you cannot test if file name contains only digits
[...]
Not with standard globbing, but with ksh globbing (enabled in
bash with shopt -s extglob and in zsh with setopt kshglob) or
with zsh globbing (with extendedglob), you can.
ksh: *([0-9])
zsh: [0-9]# or <-> ( being positive decimal integers
ranging from x to y).
--
Stéphane
Re: rm files not matching (tricky) !!
am 09.09.2007 14:49:56 von Axel Schlicht
On Sat, 08 Sep 2007 20:24:38 -0700, onkar wrote:
> I have large number of files in a directory with filenames
> containing
>
> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> might be 1232344saasderv while others might have name awer345345ds
> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>
> and
>
> (2) some files containing only digits [0-9] .
>
>
> I want to write a script removing only those files that contain both
> alphabets and digits. One trick might me to use only property (2) to
> remove files with property (1). (this is like grep -v grep )
>
> Please help me /
You could use ls -1 to produce a list of filenames, parse this list through
an awk / perl / sed script or a compiled program to produce another list
containing all the file names to delete and use this list as an input for a
shell script doing the deleting operation with a while read ... < that_list
loop.
Axel
Re: rm files not matching (tricky) !!
am 10.09.2007 01:56:50 von William James
On Sep 9, 5:31 am, Stephane CHAZELAS wrote:
> 2007-09-09, 12:04(+02), Michal Nazarewicz:
> [...]> As far as I understand your question you cannot do such a thing. As far
> > as I know you cannot test if file name contains only digits
>
> [...]
>
> Not with standard globbing, but with ksh globbing (enabled in
> bash with shopt -s extglob and in zsh with setopt kshglob) or
> with zsh globbing (with extendedglob), you can.
>
> ksh: *([0-9])
> zsh: [0-9]# or <-> ( being positive decimal integers
> ranging from x to y).
ruby -e 'puts Dir["*"].grep(/^\d+$/)'
Re: rm files not matching (tricky) !!
am 13.09.2007 12:56:46 von Michal Nazarewicz
> On Sat, 08 Sep 2007 20:24:38 -0700, onkar wrote:
>> I have large number of files in a directory with filenames
>> containing
>>
>> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
>> might be 1232344saasderv while others might have name awer345345ds
>> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>>
>> and
>>
>> (2) some files containing only digits [0-9] .
>>
>>
>> I want to write a script removing only those files that contain both
>> alphabets and digits. One trick might me to use only property (2) to
>> remove files with property (1). (this is like grep -v grep )
Axel Schlicht writes:
> You could use ls -1 to produce a list of filenames, parse this list through
> an awk / perl / sed script or a compiled program to produce another list
> containing all the file names to delete and use this list as an input for a
> shell script doing the deleting operation with a while read ... < that_list
> loop.
That brings a problem of new lines in file names.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--