grep command

gudii9
gudii9 used Ask the Experts™
on
zgrep 'xyz'  abc.log.2018111212| grep '|[1-9][0-9][0-9][0-9][0-9]|0000' | wc -l

i am trying to figure out what above grep doing?

what is meaning of
|[1-9][0-9][0-9][0-9][0-9]|0000

wc -l means some kind of word count?
what is -l

any good links or resources or video tutorials to master greps and awk and sed end to end to debug server logs?

please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007
Commented:
Each [ ] pair means single char any of digits from 0 to 9 or for rest 1 to 9 repeated 3 times
| means OR
0000 means string of 4 0 at least
-l means count words
It is a regular expression matching digits

refer to this link
Top Expert 2007

Commented:
| between commands means pass output to next command
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

nociSoftware Engineer
Distinguished Expert 2018

Commented:
grep might need to be egrep or grep -e to actualy understand the  extended regex.
wc -l means count lines not characters or words.
(without options you get a count for characters, words and lines from a file).

Author

Commented:
zgrep 'xyz'  abc.log.2018111212| grep '|[1-9][0-9][0-9][0-9][0-9]|0000' | wc -l

Open in new window

so we are searching on xyz on that log file called abc.log.2018111212


on top of results we are grepping for what numbers?

[1-9][0-9][0-9][0-9][0-9 ??

to find word count

Author

Commented:
it is like greek and latin to me

Author

Commented:
If you're using the regular expression to validate input, you'll probably want to check that the entire input consists of a valid number. To do this, replace the word boundaries with anchors to match the start and end of the string: ^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$.

Here are a few more common ranges that you may want to match:

000..255: ^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
0 or 000..255: ^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
0 or 000..127: ^(0?[0-9]?[0-9]|1[01][0-9]|12[0-7])$
0..999: ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
000..999: ^[0-9]{3}$
0 or 000..999: ^[0-9]{1,3}$
1..999: ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$
001..999: ^(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$
1 or 001..999: ^(0{0,2}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9])$
0 or 00..59: ^[0-5]?[0-9]$
0 or 000..366: ^(0?[0-9]?[0-9]|[1-2][0-9][0-9]|3[0-5][0-9]|36[0-6])$

Open in new window

not clear on above either
nociSoftware Engineer
Distinguished Expert 2018

Commented:
if using egrep:  you are searching for empty string or  4 digistring (exclude first number of 0)  or 0000 in the line.

 using grep : search the log file for xyz then search in those result for '|xyyy|0000'
xyyy  x is a digit from 1 to 9 inclusive, the y's are digits from  0 to 9 inclusive.

and then counting lines that matched the last grep.
Software Engineer
Distinguished Expert 2018
Commented:
regular expresions are a method for searching data in strings...

^ start of string
$ end of string
. match anything
[] match a class of things
? match previous entryi 0 or 1 time
* match previous entry 0 or more times
+ match previous entry 1 or more times
{x}  = match previous {x,x} times
{x,y} = match previous expression minimal x and maximal y times.
anything else matches itself.

Classes:
[a-z] = a character range from lowercase a to lowercase z  including a and z.
[0-9] = [0123456789] = match one singe digit.
[;alnum;] == [0-9a-zA-Z]  = alphabetic or numeric item

for more info see:
https://perldoc.perl.org/perlre.html
https://www.tutorialspoint.com/python/python_reg_expressions.htm

Author

Commented:
is any online tool where i can execute these commands and understand clearly what you guys mentioning
nociSoftware Engineer
Distinguished Expert 2018

Commented:
Here are 2 examples for rexes testing.
https://regex101.com/
https://regexr.com/
Distinguished Expert 2017
Commented:
the first part is looking for a pattern of xyz in the compressed log.
Then each line is passed to the next group that is looking for a numerical pattern of |10000-99999|0000
Without seeing what the results of the first query, it is hard to see what your second part is looking for and whether the pattern is right.
| can be use as a logical OR.

wc is a counter of bytes, words, new lines (effectively lines, distinction deals with any wrapped lines visually) count as one long line...).

i.e. if you write a paragraph that wraps when you look at the output it seems like four or five lines, while in actuality it is a single long line. The wrapping and visual representation of the lines depends on the settings of the page margins.

Please when you post similar question, please provide a sample of the data being analyzed, and highlight the information you are trying to get as the end result.

in the current scheme you can get 0 as a response, i.e. no lines founds mathing your criteria.
The sequence you are using is an AND ie the first part has to match and the second patter has to match, then it will be counted.

As other pointed out, egrep, can process a comparison
https://www.thegeekstuff.com/2011/10/grep-or-and-not-operators

the [1-9][0-9]{4} is an equivalent pattern the [1-9][0-9][0-9][0-9][0-9]
[] means a single literal match whatever you place in there is interpreted literally since - has a special meaning as a range, if you need to match a dash, you have to escape (\) as a \-..

When I learned how to handle regular expression, the simplest thing I found was to go from left to right and run each section.
To see what is the output from zgrep.
Then I pass this output to grep
then I pass the resulting output to the counter (wc -l)
each stage you can see what you get and what is being evaluated.....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial