Home > Archive > Unix Programming > February 2007 > shell script
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]
|
|
| amit.atray@gmail.com 2007-02-15, 8:08 am |
| Hi
I m new to scripting.......
How to grep for more than particular number say 200 in all the
directories, subdirectories and files
i want only those lines in anything more than this number is there
like
asldfjasld201 asdf
251 asdfasdf
| |
| Pascal Bourguignon 2007-02-15, 7:08 pm |
| amit.atray@gmail.com writes:
> Hi
>
> I m new to scripting.......
>
> How to grep for more than particular number say 200 in all the
> directories, subdirectories and files
>
> i want only those lines in anything more than this number is there
> like
>
> asldfjasld201 asdf
> 251 asdfasdf
This isn't really the right tool to do that, but since you can build a
regular expression matching any number written in decimal system,
greater than a given number, let's do it.
So what's a number greater than 200?
It's a number with 4 digits or more (the first being non 0)
OR a number with 3 digits with the first being [3-9]
OR a number with 3 digits with the first being 2 but the following being non 0.
\([1-9][0-9][0-9][0-9][0-9]*\|[3-9][0-9][0-9]\|2[1-9][1-9]\)
Now the question is whether you want to catch also the lines like:
asdf0.001020
?
--
__Pascal Bourguignon__ http://www.informatimago.com/
In a World without Walls and Fences,
who needs Windows and Gates?
| |
| amit.atray@gmail.com 2007-02-16, 4:13 am |
| On Feb 15, 7:23 pm, Pascal Bourguignon <p...@informatimago.com> wrote:
> amit.at...@gmail.com writes:
>
>
>
>
>
> This isn't really the right tool to do that, but since you can build a
> regular expression matching any number written in decimal system,
> greater than a given number, let's do it.
>
> So what's a number greater than 200?
>
> It's a number with 4 digits or more (the first being non 0)
> OR a number with 3 digits with the first being [3-9]
> OR a number with 3 digits with the first being 2 but the following being non 0.
>
> \([1-9][0-9][0-9][0-9][0-9]*\|[3-9][0-9][0-9]\|2[1-9][1-9]\)
>
> Now the question is whether you want to catch also the lines like:
>
> asdf0.001020
> ?
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> In a World without Walls and Fences,
> who needs Windows and Gates?
I think there is some confusion, i will like to reframe the
querry........sorry for inconvineance...
I want to search for number > X (e.g. 200) in all the
files,directories and subdirectories
I want whole number only(it should not include abc2000asdf but should
include abc 2000 asdf )
--like whole word search using grep,
| |
| Reinhard Skarbal 2007-02-16, 8:08 am |
|
<amit.atray@gmail.com> schrieb im Newsbeitrag
news:1171603450.517949.95100@a75g2000cwd.googlegroups.com...
> On Feb 15, 7:23 pm, Pascal Bourguignon <p...@informatimago.com> wrote:
>
> I think there is some confusion, i will like to reframe the
> querry........sorry for inconvineance...
> I want to search for number > X (e.g. 200) in all the
> files,directories and subdirectories
> I want whole number only(it should not include abc2000asdf but should
> include abc 2000 asdf )
> --like whole word search using grep,
>
>
Searching for X > 200 inside the filename ? or inside the file ?
Searching for X > 200 in the filename is easy :
find . -type f -exec awk '{....}' \{\} \;
where ... is a tiny awk-script.
I have no awk yet.
If you're interested in this solution, please ask me again.
Regards
Reinhard
| |
| Christopher Layne 2007-02-16, 8:08 am |
| Reinhard Skarbal wrote:
[color=darkred]
>
> <amit.atray@gmail.com> schrieb im Newsbeitrag
Given that he gave an example with spaces, I doubt he was referring to
filenames even though technically it's allowed.
Modern GNU egrep method:
egrep -w -r / '[2-9][0-9][0-9]|[0-9][0-9][0-9][0-9]+'
| |
| Pascal Bourguignon 2007-02-16, 8:08 am |
| amit.atray@gmail.com writes:
> On Feb 15, 7:23 pm, Pascal Bourguignon <p...@informatimago.com> wrote:
>
> I think there is some confusion, i will like to reframe the
> querry........sorry for inconvineance...
> I want to search for number > X (e.g. 200) in all the
> files,directories and subdirectories
> I want whole number only(it should not include abc2000asdf but should
> include abc 2000 asdf )
> --like whole word search using grep,
I shown you as an example, how to build a regexp to search for number>200.
I hope you can build regexps for any X following this example.
So the question is how to build a regular expression matching numbers
greater than N.
A regexp that matches a number greater than N is:
a regexp that matches a number with more digits than N
OR a regexp that matches a number with the same number of digits as N
but with the first digit greater than the first digit of N
OR a regexp that matches a number with the same number of digits as N
with the same first digit,
but with the next digit greater than the corresponding digit of N
etc...
Therefore:
(defun build-greater-than-regexp (N)
(labels ((regexp-or (regexps) (format nil "\\(~{~A~^\\|~}\\)" regexps))
(greater-same-length (left-digits right-digits)
(if (endp (rest right-digits))
(if (= 9 (first right-digits))
(quote ())
(list (format nil "~{~D~}[~D-9]"
left-digits
(1+ (first right-digits)))))
(if (= 9 (first right-digits))
(greater-same-length
(append left-digits (list (first right-digits)))
(rest right-digits))
(cons (format nil "~{~D~}~D~{~*[0-9]~}"
left-digits
(1+ (first right-digits))
(rest right-digits))
(greater-same-length
(append left-digits (list (first right-digits)))
(rest right-digits)))))))
(regexp-or
(cons (format nil "[1-9]~:[~;[0-9]\\{~D\\}~]"
(<= 10 N) (ceiling (log N 10)))
(greater-same-length (quote ())
(map (quote list) (function digit-char-p)
(format nil "~D" N)))))))
C/USER[18]> (BUILD-GREATER-THAN-REGEXP 200)
"\\([1-9][0-9]\\{3\\}\\|3[0-9][0-9]\\|21[0-9]\\|20[1-9]\\)"
C/USER[19]> (BUILD-GREATER-THAN-REGEXP 201)
"\\([1-9][0-9]\\{3\\}\\|3[0-9][0-9]\\|21[0-9]\\|20[2-9]\\)"
C/USER[20]> (BUILD-GREATER-THAN-REGEXP 209)
"\\([1-9][0-9]\\{3\\}\\|3[0-9][0-9]\\|21[0-9]\\)"
C/USER[21]> (BUILD-GREATER-THAN-REGEXP 291)
"\\([1-9][0-9]\\{3\\}\\|3[0-9][0-9]\\|29[2-9]\\)"
C/USER[22]> (BUILD-GREATER-THAN-REGEXP 299)
"\\([1-9][0-9]\\{3\\}\\|3[0-9][0-9]\\)"
So you could write:
#!/bin/bash
X=$1
file=$2
regexp=$(clisp -norc -q -x '
(progn
(defun build-greater-than-regexp (N)
(labels ((regexp-or (regexps) (format nil "\\(~{~A~^\\|~}\\)" regexps))
(greater-same-length (left-digits right-digits)
(if (endp (rest right-digits))
(if (= 9 (first right-digits))
(quote ())
(list (format nil "~{~D~}[~D-9]"
left-digits
(1+ (first right-digits)))))
(if (= 9 (first right-digits))
(greater-same-length
(append left-digits (list (first right-digits)))
(rest right-digits))
(cons (format nil "~{~D~}~D~{~*[0-9]~}"
left-digits
(1+ (first right-digits))
(rest right-digits))
(greater-same-length
(append left-digits (list (first right-digits)))
(rest right-digits)))))))
(regexp-or
(cons (format nil "[1-9]~:[~;[0-9]\\{~D\\}~]"
(<= 10 N) (ceiling (log N 10)))
(greater-same-length (quote ())
(map (quote list) (function digit-char-p)
(format nil "~D" N)))))))
'"(princ (build-greater-than-regexp $X)) (values))")
grep -e "$regexp" $file
The problem is that grep is not Turing Complete. You could do
everything in sed if you don't want to go thru lisp to build the
regexp; sed is Turing Complete...
--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot. don't bother saving your artefacts.
| |
| Stephane CHAZELAS 2007-02-18, 7:04 pm |
| 2007-02-15, 04:13(-08), amit.atray@gmail.com:
[...]
> I m new to scripting.......
Did you know there was a news group called comp.unix.shell?
> How to grep for more than particular number say 200 in all the
> directories, subdirectories and files
>
> i want only those lines in anything more than this number is there
> like
>
> asldfjasld201 asdf
> 251 asdfasdf
find . -type f -exec perl -ne '
print if grep { $_ > 200 } (/\d+/g)' {} +
--
Stephane
|
|
|
|
|