ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The [] in regular expressions does not mean integer range, it means a list of characters, e.g. [abc] matches a b or c. The [a-c] is a short way of saying "all the characters between a and c inclusive", and works with letters and numbers. [12-23] therefore matches the single character 1, 2, or 3 (because "2-2" is shortened to simply "2"). The * is to be used after a pattern, not before, and indicates "0 or more occurrences of the previous pattern).
You could split the pattern up a little to help the thinking process.
You could provide each of these patterns separately using multiple -e options to grep, and enclosing the patterns in quotes - if you happen to have a file in the working directory whose names matches the pattern you are grepping for, the shell will do the substitution and then pass the file name to grep as the pattern, which can cause nasty unexpected behaviour.
You could also consolidate them into a single pattern, using (pattern1|pattern2|pattern3) for the parts which vary, and use egrep, which supports this extended RE type. Personally I prefer using multiple -e options as it is clearer, but if you want to do it all in one, here's how:
I know why it doesn't work, but don't do enough with regular expressions to tell you how to fix it. You problem is that a regular expression does not understand two digit numbers. You can tell it 12, but it is looking litterly for 1 followed by a 2. It doesn't start at 12 and look for 13, 14, 15, ... it just doesn't work that way. You might want something like ...
Code:
egrep '(1[2-9]|2[0-3])'
That should translate to 12-23, though as I've said, I don't use regular expressions often.
Maybe a little off topic, but if you have a list of process names in glist, you can shorten the ps command as in
Code:
ps --no-headers -C $i -o etime
--no-headers do not print the header line
-C search processes by command name
and in the output part you can put only etime. In this way you don't need the grep -v httpd |grep -v sidd | awk '{print $3}' part.
The [] in regular expressions does not mean integer range, it means a list of characters, e.g. [abc] matches a b or c. The [a-c] is a short way of saying "all the characters between a and c inclusive", and works with letters and numbers. [12-23] therefore matches the single character 1, 2, or 3 (because "2-2" is shortened to simply "2"). The * is to be used after a pattern, not before, and indicates "0 or more occurrences of the previous pattern).
You could split the pattern up a little to help the thinking process.
You could provide each of these patterns separately using multiple -e options to grep, and enclosing the patterns in quotes - if you happen to have a file in the working directory whose names matches the pattern you are grepping for, the shell will do the substitution and then pass the file name to grep as the pattern, which can cause nasty unexpected behaviour.
You could also consolidate them into a single pattern, using (pattern1|pattern2|pattern3) for the parts which vary, and use egrep, which supports this extended RE type. Personally I prefer using multiple -e options as it is clearer, but if you want to do it all in one, here's how:
Could you do me a favor now and explain that last code segment to me so I can learn to fish?
He kind of spells it out in the first section of code. The only difference is that instead of being three separate statements, he condenses it down to one statement. This was done by taking everything that as dis-similar (the days and hours), and putting them in parenthesis, and everything that was similar (minutes/seconds), and keeping them outside of the parenthesis. The parenthesis creates a group that regexp interprets as "one of these" (these separated by a bar '|'), followed by the rest of the expression.
More formally, (pattern1|pattern2|pattern3) matches if pattern1 or pattern2 or pattern3 matches. The | is a logical or, the parenthesis group the sub-patterns in the or.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.