ls with ignore pattern

ls with ignore pattern

Post by Brian Seppane » Sat, 07 Apr 2001 00:10:40



I've got a directory that has a lot of files, and some of these files
correspond to ip addresses.
I want to list only the files in this directory that are not IP
addresses but have actual names.
The ls -I option appears to be what I need.  It will ignore a specified
pattern, but I'm not having much luck getting that pattern to match my
scenario.

What I've tried is
ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

but this doesn't work.  There doesn't seem to be much coverage of how to
express the pattern.  This may be the only problem with it, but I'm not
sure what that would be.

The man page states the -I option this way "do not list implied entries
matching shell PATTERN"

Has anyone used this option with success?

Any help appreciated.
Thanks,
Brian Seppanen

 
 
 

ls with ignore pattern

Post by Kirk Ro » Sat, 07 Apr 2001 00:16:42



>I've got a directory that has a lot of files, and some of these files
>correspond to ip addresses.
>I want to list only the files in this directory that are not IP
>addresses but have actual names.
SNIP
>What I've tried is
>ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

>but this doesn't work.  There doesn't seem to be much coverage of how to
>express the pattern.  This may be the only problem with it, but I'm not
>sure what that would be.

How about just piping to grep -v

Kirk

 
 
 

ls with ignore pattern

Post by Anand Harihara » Sat, 07 Apr 2001 04:45:13


Are the filenames themselves IP addresses or do they *CONTAIN* IP addresses?



> >I've got a directory that has a lot of files, and some of these files
> >correspond to ip addresses.
> >I want to list only the files in this directory that are not IP
> >addresses but have actual names.
> SNIP
> >What I've tried is
> >ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

> >but this doesn't work.  There doesn't seem to be much coverage of how to
> >express the pattern.  This may be the only problem with it, but I'm not
> >sure what that would be.

> How about just piping to grep -v

> Kirk

 
 
 

ls with ignore pattern

Post by Brian Seppane » Sat, 07 Apr 2001 05:02:43


Quote:> Are the filenames themselves IP addresses or do they *CONTAIN* IP addresses?

The filenames start with IP addresses.  Basically they are reverse DNS
host files.
192.168.0.1.hosts, etc.

Thanks,
Brian Seppanen

 
 
 

ls with ignore pattern

Post by Mike Moh » Sat, 07 Apr 2001 08:12:25



> How about just piping to grep -v

> Kirk

Don't you mean egrep?  Only the gnu version of grep allows pattern matching
of the sort that Brian requested.  Otherwise, you want to pipe to egrep.

--

================================================================

Information Technology Group      === Phone: 64 9 917-9999 x8133
Auckland University of Technology === Fax:   64 9 917-9901
PO Box 92006, Auckland, New Zealand =
http://home.aut.ac.nz/staff/mmohr/
================================================================
If we have unlimited ability to think, why do we use computers?

 
 
 

ls with ignore pattern

Post by laura fairhe » Sat, 07 Apr 2001 08:07:44



>I've got a directory that has a lot of files, and some of these files
>correspond to ip addresses.
>I want to list only the files in this directory that are not IP
>addresses but have actual names.
>The ls -I option appears to be what I need.  It will ignore a specified
>pattern, but I'm not having much luck getting that pattern to match my
>scenario.

>What I've tried is
>ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

>but this doesn't work.  There doesn't seem to be much coverage of how to
>express the pattern.  This may be the only problem with it, but I'm not
>sure what that would be.

>The man page states the -I option this way "do not list implied entries
>matching shell PATTERN"

>Has anyone used this option with success?

cat <<\$L

There are a couple of errors in your pattern;

(i)  Using '[1-9]' means that any IP address that has
     a zero in one of the decimals will be not be matched.

(ii) The dot ('.') character is a special character
     in a regular expression meaning 'match any character'
     to match a literal dot you need to escape it.

How about something like;

ls |grep -v '^[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*'

$L

- Show quoted text -

>Any help appreciated.
>Thanks,
>Brian Seppanen


 
 
 

ls with ignore pattern

Post by t.. » Sat, 07 Apr 2001 11:25:37



> I've got a directory that has a lot of files, and some of these files
> correspond to ip addresses.
> I want to list only the files in this directory that are not IP
> addresses but have actual names.
> The ls -I option appears to be what I need.  It will ignore a specified
> pattern, but I'm not having much luck getting that pattern to match my
> scenario.

> What I've tried is
> ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

> but this doesn't work.

That's because it uses shell (glob) patterns, not regular expressions.
Although that wouldn't be correct as regexp either, if you want literal
periods in there.

Quote:>The filenames start with IP addresses.  Basically they are reverse DNS
>host files.
>192.168.0.1.hosts, etc.

Your example above only looks for three parts, is that intentional?
Do you want to skip also files like 192.168.0.hosts?
Anyway, you could do that with multiple --ignore options, but it's gets
really unwieldy:

ls --ignore '[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].*' \
--ignore '[0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].*' ...

You'd need 3^3=27 different patterns for 3 parts, 3^4=81 for four. Ouch.
With ksh you could do

ls !([0-9]?([0-9])?([0-9]).[0-9]?([0-9])?([0-9]).[0-9]?([0-9])?([0-9])*)

or if you could relax the test a little and skip also files with
more than three digits in each field,

ls !(+([0-9]).+([0-9]).+([0-9])*)

It's probably better to use grep, however:

ls | grep -Ev '^([0-9]{1,3}\.){3}'

Note single quotes and the leading ^.
That assumes a period after the IP, if you want to catch names
like "192.168.1fish" as well, try

ls | grep -Ev '^([0-9]{1,3}\.){2}[0-9]{1,3}'

By the way, that's still catching patterns that can't be
IP addresses, like 333.444.555. If those are not
possible, it might be worthwhile to think what exactly
are possible, it could be possible to simplify the
pattern more. On the other hand if you want to
see those, that is exclude only numbers 0-255,
it gets more complicated:

ls | grep -Ev '^((1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}'

That should catch only numbers 0-255 in each part
(untested).

--
Tapani Tarvainen

 
 
 

ls with ignore pattern

Post by Robert Sip » Sat, 07 Apr 2001 13:25:59


I guess I would use a "ls -l [a-zA-Z]*" pattern.  If it is not narrow enough
I would add other pattern limits to it.


> I've got a directory that has a lot of files, and some of these files
> correspond to ip addresses.
> I want to list only the files in this directory that are not IP
> addresses but have actual names.
> The ls -I option appears to be what I need.  It will ignore a specified
> pattern, but I'm not having much luck getting that pattern to match my
> scenario.

> What I've tried is
> ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

> but this doesn't work.  There doesn't seem to be much coverage of how to
> express the pattern.  This may be the only problem with it, but I'm not
> sure what that would be.

> The man page states the -I option this way "do not list implied entries
> matching shell PATTERN"

> Has anyone used this option with success?

> Any help appreciated.
> Thanks,
> Brian Seppanen


 
 
 

ls with ignore pattern

Post by Piotrek Kapczu » Sat, 07 Apr 2001 20:28:59





> > I've got a directory that has a lot of files, and some of these files
> > correspond to ip addresses.
> > I want to list only the files in this directory that are not IP
> > addresses but have actual names.
> > The ls -I option appears to be what I need.  It will ignore a specified
> > pattern, but I'm not having much luck getting that pattern to match my
> > scenario.

> > What I've tried is
> > ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

> > but this doesn't work.

[...]

Quote:> It's probably better to use grep, however:

> ls | grep -Ev '^([0-9]{1,3}\.){3}'

[ ... ls|grep examples ... ]

When there are lot of files to display, I suggest to use 'find . ! -name
X -ls' rather than 'ls|grep -v'. It seems to be faster.

--
Regards
Piotrek Kapczuk

 
 
 

ls with ignore pattern

Post by Brian Seppane » Sun, 08 Apr 2001 03:42:42


Quote:> I guess I would use a "ls -l [a-zA-Z]*" pattern.  If it is not narrow enough
> I would add other pattern limits to it.

Doh!!!!!  The kiss principle is alive and well.  Keep it simple stupid,
just about sums it up.
That was exactly what I needed.  Not once did I consider that option.
jeez.

Thanks,
Brian Seppanen

 
 
 

ls with ignore pattern

Post by Bill Vermilli » Sun, 08 Apr 2001 05:34:17




Quote:

>> Are the filenames themselves IP addresses or do they *CONTAIN* IP addresses?

>The filenames start with IP addresses.  Basically they are reverse DNS
>host files.
>192.168.0.1.hosts, etc.

So if it's just files starting the numbers which you don't want to
see how about ls -la [A-z]*

--

 
 
 

ls with ignore pattern

Post by Bill Vermilli » Sun, 08 Apr 2001 05:38:39




>I guess I would use a "ls -l [a-zA-Z]*" pattern. If it is not
>I narrow enough would add other pattern limits to it.

Since - in the standard default English distribution where A thru Z
sort before a thru Z you could just ls -la [A-z]*, which would
include any files starting between Z and a. IOW file names
starting with [ \ ] ^ _ and `, which you'd really probably want to
know about anyway.  I lk brvty



>> I've got a directory that has a lot of files, and some of these files
>> correspond to ip addresses.
>> I want to list only the files in this directory that are not IP
>> addresses but have actual names.
>> The ls -I option appears to be what I need.  It will ignore a specified
>> pattern, but I'm not having much luck getting that pattern to match my
>> scenario.

>> What I've tried is
>> ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

>> but this doesn't work.  There doesn't seem to be much coverage of how to
>> express the pattern.  This may be the only problem with it, but I'm not
>> sure what that would be.

>> The man page states the -I option this way "do not list implied entries
>> matching shell PATTERN"

>> Has anyone used this option with success?

>> Any help appreciated.
>> Thanks,
>> Brian Seppanen


--

 
 
 

ls with ignore pattern

Post by Akop Pogosia » Tue, 10 Apr 2001 08:29:20



Quote:> I've got a directory that has a lot of files, and some of these files
> correspond to ip addresses.
> I want to list only the files in this directory that are not IP
> addresses but have actual names.
> The ls -I option appears to be what I need.  It will ignore a specified
> pattern, but I'm not having much luck getting that pattern to match my
> scenario.
> What I've tried is
> ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"

Uh, are you sure that the ls that you use supports those options?
--ignore and -I exist in GNU ls but not in the Solaris version of ls.
Either you have both installed on the same system and your MANPATH is
messed up or you assume that it will work the same as on a Linux
system. If you do want to use GNU ls when it is not the first
thing on your PATH (which is correct) call it using the complete
pathname to it (such as /usr/local/bin/ls or wherever you have it
installed).

-akop

 
 
 

1. ls pattern with xargs


   >The above is all correct ... but sometimes overkill.
   >Remember that you can always use wildcards etc.
   >eg.
   >ls *txt  -- list all files ending in txt
   >ls [fb]oo -- lists files foo and boo
   >I don't know where you may find a definitive list
   >of wildcards etc. try man ksh/sh/csh depending on the
   >shell you are using.
The original question was what to do when the list is too long.  If you have
thousands of files in one directory you may need to use ls | grep.

Net-Tamer V 1.08X - Test Drive

2. How to add /dev/hdi ... /dev/hdp

3. ls <pattern> with xargs

4. PCMCIA on UDB (Problem identified)

5. /usr/bin/ls /usr/ucb/ls /usr/local/bin/ls

6. Postfix troubles

7. ls cmd to ignore "incomplete" files?

8. web-base smit is very slow

9. ls option to ignore case

10. Pattern Matching Not Working When Pattern Assign To A Variable

11. sed pattern replacement question with pattern duplication

12. ksh pattern matching when pattern is in a variable

13. grepping for pattern, and then deleting ALL the following lines after the pattern