0

have a log file , having multiple contents inside the file like and example below is the content of U123.log . I have n number of logs..

Accepted password for hoover from 10.0.2.2 port 4792 ss

Id : UN123P 

ID_MTCH : UnixProduction

Accepted password for hoover from 10.0.2.2 port 4792 ss

Accepted password for hoover from 10.0.2.2 port 4792 ss

Is there any we can fetch Id : UN123P & ID_MTCH from each log as an output of linux command. We have some 1000's of logs and from all the logs , trying to fetch the value only starts with ID : & ID_MTCH and sample output as below

Id : UN123P 

ID_MTCH : UnixProduction

Pls suggest

| improve this question | | | | |
1

Depending on whether the pattern can also occur later in log lines (and not only at the beginning), you may have to use anchored regular expression with grep, as in

grep '^\(Id\|ID_MTCH\)' *.log

or

grep -E '^(Id|ID_MTCH)' *.log

This will then only match lines that begin (as first character, without leading space etc.!) with the pattern mentioned.

| improve this answer | | | | |
1

You could use grep for that

grep -i -E 'ID|ID_MTCH' *.log

| improve this answer | | | | |
  • Thanks Lawrence for the reply , i missed to add a point in the query , I just modified my question now – Arya May 20 at 6:44
  • @Arya Updated my answer to match – Lawrence May 21 at 4:19
0

man grep says

 grep  searches  for PATTERNS in each FILE.  PATTERNS is one or patterns
       separated by newline characters, and grep prints each line that matches
       a pattern.

 grep [OPTION...] PATTERNS [FILE...]

 -i, --ignore-case
              Ignore case distinctions, so that characters that differ only in
              case match each other.

Use the following code

grep -i ID *.log

-i is important as it makes the grepping independent of case.

| improve this answer | | | | |
0

To match both cases, you can use extended grep (egrep) as well. This works the same way as grep -E would.

egrep -i -r '^Id|^id_match' *.log
| improve this answer | | | | |
-1

To match both lines you can use:

grep -E 'Id :|ID_MTCH' *.log
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.