0

I want to capture all the lines that have the (XXXX) word before the last word

while xxxxx - is number

 /opt/OV/bin/opcagt -status
 scopeux     Perf Agent data collector                        (5102)   Running
 midaemon    Measurement Interface daemon                     (5110)   Running
 ttd         ARM registration daemon                                   Running
 perfalarm   Alarm generator                                  (5111)   Running
 agtrep      OV Discovery Agent                  AGENT,AgtRep (5520)   Running
 coda        OV Performance Core                 COREXT       (5529)   Running
 opcacta     OVO Action Agent                    AGENT,EA     (5427)   Running
 opcle       OVO Logfile Encapsulator            AGENT,EA     (5443)   Running
 opcmona     OVO Monitor Agent                   AGENT,EA              Running
 opcmsga     OVO Message Agent                   AGENT,EA     (5435)   Running
 opcmsgi     OVO Message Interceptor             AGENT,EA     (5553)   Running
 ovbbccb     OV Communication Broker             CORE         (5352)   Running
 ovcd        OV Control                          CORE         (5344)   Running
 ovconfd     OV Config and Deploy                COREXT       (5383)   Running

I try with

/opt/OV/bin/opcagt -status | grep [0-9]

but this grep syntax not capture the word before the last word

expected results:

scopeux     Perf Agent data collector                        (5102)   Running
midaemon    Measurement Interface daemon                     (5110)   Running
perfalarm   Alarm generator                                  (5111)   Running
agtrep      OV Discovery Agent                  AGENT,AgtRep (5520)   Running
coda        OV Performance Core                 COREXT       (5529)   Running
opcacta     OVO Action Agent                    AGENT,EA     (5427)   Running
opcle       OVO Logfile Encapsulator            AGENT,EA     (5443)   Running
opcmsga     OVO Message Agent                   AGENT,EA     (5435)   Running
opcmsgi     OVO Message Interceptor             AGENT,EA     (5553)   Running
ovbbccb     OV Communication Broker             CORE         (5352)   Running
ovcd        OV Control                          CORE         (5344)   Running
ovconfd     OV Config and Deploy                COREXT       (5383)   Running
  • 1
    actually, for your sample input and expected output, grep [0-9] should work.. can you post exact output you get and how it differs from expected output? – Sundeep Sep 19 '16 at 10:53
  • 1
    @spasic [0-9] path might be expanded by shell if a file match that pattern. – Archemar Sep 19 '16 at 11:46
  • @Archemar, I tried by creating dummy file 1 and indeed it affects the results when regex is not quoted.. probably add that to your answer :) – Sundeep Sep 19 '16 at 11:59
0

try

/opt/OV/bin/opcagt -status |  awk 'NF>2 && $(NF-1) ~ /\([0-9]*\)/ '

where

  • $(NF-1) match before last field
  • ~ ask awk to do a pattern matching
  • /\([0-0]*\)/ pattern is (, any number of digit and ) (you can use [0-9][0-9]* to have at least one.
  • default action is to print.
  • on some lines I get this "awk: (FILENAME=- FNR=11) fatal: attempt to access field -1" – yael Sep 19 '16 at 11:00
  • this must be empty lines, I edited my post. – Archemar Sep 19 '16 at 11:05
2

To search for lines containing pattern (4-digits)<spaces>word at end of line

grep -E '\([0-9]{4}\)\s*\w+$'
  • I think there should be at least one space and just one number. Here is only the closing parantese used as identifier: grep -E '[0-9]\)\s+\w' – hschou Sep 19 '16 at 10:58
  • very nice But -E isnt part of my grep command – yael Sep 19 '16 at 11:32
  • @Yael Do you have /bin/egrep ? That is the same. – hschou Sep 19 '16 at 12:14
0

You can do:

... | grep -E '[[:blank:]]\([0-9]{4}\)[[:blank:]]+[^[[:blank:]]+$'
  • [[:blank:]]\([0-9]{4}\) matches a whitespace, followed by 4 digits

  • [[:blank:]]+[^[[:blank:]]+$ matches one or more whitespaces followed by one or more non-whitespace characters, at the end.

Example:

$ cat file.txt
scopeux     Perf Agent data collector                        (5102)   Running
 midaemon    Measurement Interface daemon                     (5110)   Running
 ttd         ARM registration daemon                                   Running
 perfalarm   Alarm generator                                  (5111)   Running
 agtrep      OV Discovery Agent                  AGENT,AgtRep (5520)   Running
 coda        OV Performance Core                 COREXT       (5529)   Running
 opcacta     OVO Action Agent                    AGENT,EA     (5427)   Running
 opcle       OVO Logfile Encapsulator            AGENT,EA     (5443)   Running
 opcmona     OVO Monitor Agent                   AGENT,EA              Running
 opcmsga     OVO Message Agent                   AGENT,EA     (5435)   Running
 opcmsgi     OVO Message Interceptor             AGENT,EA     (5553)   Running
 ovbbccb     OV Communication Broker             CORE         (5352)   Running
 ovcd        OV Control                          CORE         (5344)   Running
 ovconfd     OV Config and Deploy                COREXT       (5383)   Running

$ grep -E '[[:blank:]]\([0-9]{4}\)[[:blank:]]+[^[[:blank:]]+$' file.txt
scopeux     Perf Agent data collector                        (5102)   Running
 midaemon    Measurement Interface daemon                     (5110)   Running
 perfalarm   Alarm generator                                  (5111)   Running
 agtrep      OV Discovery Agent                  AGENT,AgtRep (5520)   Running
 coda        OV Performance Core                 COREXT       (5529)   Running
 opcacta     OVO Action Agent                    AGENT,EA     (5427)   Running
 opcle       OVO Logfile Encapsulator            AGENT,EA     (5443)   Running
 opcmsga     OVO Message Agent                   AGENT,EA     (5435)   Running
 opcmsgi     OVO Message Interceptor             AGENT,EA     (5553)   Running
 ovbbccb     OV Communication Broker             CORE         (5352)   Running
 ovcd        OV Control                          CORE         (5344)   Running
 ovconfd     OV Config and Deploy                COREXT       (5383)   Running

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.