|
Home > Archive > Unix Programming > May 2006 > Regex to compare whether the value of a string is greater than a number
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regex to compare whether the value of a string is greater than a number
|
|
| Stevie 2006-05-08, 7:16 am |
| Hello
I'm trying to work out the regular expression for carrying out a simple
'greater than' comparison.
I have a directory with files such as asd345.log, asd346.log,
asd347.log and so on.
The regex is to be using in a find command, which should return a list
of files whose numeric portion is greater than, say, 346. In the
example above this would return one file name, asd347.log.
Thanks for any help
Stevie
| |
| Barry Margolin 2006-05-09, 1:15 am |
| In article <1147078044.422603.114940@v46g2000cwv.googlegroups.com>,
"Stevie" <steviehaston@hotmail.com> wrote:
> Hello
>
> I'm trying to work out the regular expression for carrying out a simple
> 'greater than' comparison.
>
> I have a directory with files such as asd345.log, asd346.log,
> asd347.log and so on.
>
> The regex is to be using in a find command, which should return a list
> of files whose numeric portion is greater than, say, 346. In the
> example above this would return one file name, asd347.log.
[1-9][0-9][0-9][0-9]|[^0-9]([4-9][0-9][0-9]|3([5-9][0-9]|4[7-9]))[^0-9]
The first part matches any number four or more digits long. Then it
matches 3-digit numbers starting with 4, starting with 3 followed by
5-9, and starting with 34 followed by 7-9.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Kenan Kalajdzic 2006-05-09, 1:17 pm |
| > I have a directory with files such as asd345.log, asd346.log,
> asd347.log and so on.
>
> The regex is to be using in a find command, which should return a list
> of files whose numeric portion is greater than, say, 346. In the
> example above this would return one file name, asd347.log.
If all the files reside in one directory, you don't need to use 'find'.
Simply use 'ls' and alter its output with 'sed':
ls dir | sed '1,/asd346.log/d'
--
Kenan Kalajdzic
| |
| Stevie 2006-05-10, 7:19 am |
| Thanks Kenan, the sed approach works very well. How does it do this?
(the man pages for sed arent easy to understand)
Stevie
| |
| Barry Margolin 2006-05-10, 7:19 am |
| In article <1147261698.577849.166720@i40g2000cwc.googlegroups.com>,
"Stevie" <steviehaston@hotmail.com> wrote:
> Thanks Kenan, the sed approach works very well. How does it do this?
> (the man pages for sed arent easy to understand)
> Stevie
It makes use of the fact that ls sorts the filenames. '1,/346/d'
deletes all the lines from the beginning to the one matching 346.
However, this solution only works if there really is a file whose name
contains 346. If you want to show only the filenames containing numbers
higher than 346, but the filename sequence is 342, 345, 347, 348, it
will never match 346 and never stop deleting.
Also, it only works if all the filenames contain 3-digit numbers,
because ls is sorting lexicographically, not numerically. So if you
have a two-digit number like 45 in there, it will show up in the result,
and so will a 4-digit number like 1234.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Kenan Kalajdzic 2006-05-12, 7:15 pm |
| Barry Margolin <barmar@alum.mit.edu> wrote:
>
> However, this solution only works if there really is a file whose name
> contains 346. If you want to show only the filenames containing numbers
> higher than 346, but the filename sequence is 342, 345, 347, 348, it
> will never match 346 and never stop deleting.
>
> Also, it only works if all the filenames contain 3-digit numbers,
> because ls is sorting lexicographically, not numerically. So if you
> have a two-digit number like 45 in there, it will show up in the result,
> and so will a 4-digit number like 1234.
Thank you for pointing this out. I would then take an alternative
approach to find a more general solution for the problem. It should
be possible to specify an extended regular expression as the value of
the field separator in most contemporary versions of awk. Using this
feature, it is easy to split the filenames and extract the number from
each of them, so it can be compared to a predefined constant:
ls dir | awk -F 'asd|\.log' '$2 > 346'
--
Kenan Kalajdzic
| |
| William James 2006-05-13, 1:14 pm |
| Stevie wrote:
> Hello
>
> I'm trying to work out the regular expression for carrying out a simple
> 'greater than' comparison.
>
> I have a directory with files such as asd345.log, asd346.log,
> asd347.log and so on.
>
> The regex is to be using in a find command, which should return a list
> of files whose numeric portion is greater than, say, 346. In the
> example above this would return one file name, asd347.log.
>
> Thanks for any help
> Stevie
ls dir | awk 'substr($0, match($0,/[0-9]/) ) + 0 > 346'
|
|
|
|
|