Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex to find all lines matching a pattern in Python [duplicate]

Tags:

python

regex

file

I have:

# runPath is the current path, commands is a list that's mostly irrelevant here
def ParseShellScripts(runPath, commands):
    for i in range(len(commands)):
        if commands[i].startswith('{shell}'):
            # todo: add validation/logging for directory `sh` and that scripts actually exist
            with open(os.path.join(runPath, 'sh', commands[i][7:]),"r") as shellFile:
                for matches in re.findall("/^source.*.sh", shellFile):
                    print matches

However, I get this error:

Traceback (most recent call last):
  File "veri.py", line 396, in <module>
    main()
  File "veri.py", line 351, in main
    servList, labels, commands, expectedResponse = ParseConfig(relativeRunPath)
  File "veri.py", line 279, in ParseConfig
    commands = ParseShellScripts(runPath, commands)
  File "veri.py", line 288, in ParseShellScripts
    for matches in re.findall("/^source.*.sh", shellFile):
  File "/usr/lib/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

Edit: adding some files as examples

#config.sh
#!/bin/bash

dbUser = 'user'
dbPass = 'pass'
dbSchema = ''
dbMaxCons = '4000'

#the shellFile I'm looking in
#!/bin/bash
source config.sh

OUTPUT=$(su - mysql -c "mysqladmin variables" | grep max_connections | awk '{print $4}')
if [[ ${OUTPUT} -ge ${dbMaxCons}]]; then
    echo "Success"
    echo ${OUTPUT}
else
    echo ${OUTPUT}
fi   

Basically what I want to accomplish is to search through all specefied files in the sh directory, and if any of them contain source*.sh (e.g., source config.sh), print that file (eventually I will be expanding it out and appending it to the top of the current file so that I can pass the single command-string via ssh.. but that's not relevant here I don't think.)

What am I doing wrong?

like image 408
MrDuk Avatar asked Feb 06 '26 01:02

MrDuk


1 Answers

You are trying to run a regex.findall on the file handle shellFile. You need to read from that file, and run the regex on the data you read.

Maybe, something like this?

with open(os.path.join(runPath, 'sh', commands[i][7:]),"r") as shellFile:
    data = shellFile.read()
    for matches in re.findall("/^source.*.sh", data):
        print matches
like image 185
Ankush Jain Avatar answered Feb 07 '26 13:02

Ankush Jain