Regular expression

I would like to extract "JDBC Thin Client" from (PROGRAM=JDBC Thin Client); and "C:\Python26\python.exe" from (PROGRAM=C:\Python26\python.exe); "txm_v_awsrf_db.pr" from (PROGRAM=txm_v_awsrf_db.pr).

if input line like "(PROGRAM=)", it return unknown.

Would you help suggest a regular expression pattern for me?

I am using python.

thank you very much.

So what you need it to extract whatever that comes after PROGRAM= ?

I need to extract whatever that comes after "(PROGRAM=" and before ")".In fact, this is Oracle listener log, there are some many "(XXX=YYY)", you are appreciated if you can build a function, it can return YYY when I input XXX.

A regular expression alone is a search, and results in true or false.
sed or perl have a substitute function that works with it

sed -n 's#.*(PROGRAM=\([^)]*\)).*#\1#p'

Here's something to get you started:

$
$ cat listener.log
Line 1 - blah
Line 2 - blah, blah
20-FEB-2002 14:46:54 * (CONNECT_DATA=(SERVICE_NAME=OZZIEDB001.OZZIEWORKS.COM)(CID=(PROGRAM=C:\WINNT\System32\dllhost.exe)(HOST=OZZIELIVE02)(USER=Administrator))) * (ADDRESS=(PROTOCOL=tcp)(HOST=XX.YYY.16.73)(PORT=21 32)) * establish * OZZIEDB001.OZZIEWORKS.COM * 0
Line 4 - some more stuff
Line 5 - final line here...
$
$
$ cat -n grep_listener.py
     1  #!/usr/bin/python
     2  import sys
     3  import re
     4
     5  # ===============================================
     6  # Subroutine section
     7  # ===============================================
     8  def find_value (ptrn, str):                       # function takes in the regex pattern and the string
     9      val = ""                                      # set the default return value
    10      if re.search(ptrn, str) != None:              # if pattern was found in the string
    11          val = re.sub(ptrn, r'\1', str)            # then substitute it by the placeholder and set that as return value
    12      return val                                    # return the value
    13
    14  # ===============================================
    15  # Main section
    16  # ===============================================
    17  file_name = sys.argv[1]                           # 1st argument = file name
    18  search_for = sys.argv[2]                          # 2nd argument = word to search for
    19  pattern = "^.*?\(" + search_for + "=(.*?)\).*$"   # set up the regex pattern
    20  f = open (file_name, 'r')                         # open the file
    21  for line in f:                                    # iterate through each line
    22      value = find_value(pattern, line)             # call function to return param value for this line
    23      if value != "":                               # if value was found
    24          print value,                              # then print it
    25
$
$ python grep_listener.py listener.log SERVICE_NAME
OZZIEDB001.OZZIEWORKS.COM
$
$ python grep_listener.py listener.log PROGRAM
C:\WINNT\System32\dllhost.exe
$
$ python grep_listener.py listener.log BLAH
$
$