How to search for multiple strings in a file with multiline stringgrep a file, but show several surrounding lines?How to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?How do I copy a file in Python?How do I include a JavaScript file in another JavaScript file?How to do case insensitive search in VimHow do I list all files of a directory?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?How do you append to a file in Python?
What to do when surprise and a high initiative roll conflict with the narrative?
You have (3^2 + 2^3 + 2^2) Guesses Left. Figure out the Last one
How to handle (one's own) self-harm scars (on the arm), in a work environment?
What aircraft was used as Air Force One for the flight between Southampton and Shannon?
How to hide rifle during medieval town entrance inspection?
Heap allocation on microcontroller
Who are the Missing Members of this Noble Family?
Why we don’t make use of the t-distribution for constructing a confidence interval for a proportion?
Why are trash cans referred to as "zafacón" in Puerto Rico?
Fixing obscure 8080 emulator bug?
What is the actual quality of machine translations?
Writing an augmented sixth chord on the flattened supertonic
Can I use G instead of GG (short for days) if I am out of space?
How to trick the reader into thinking they're following a redshirt instead of the protagonist?
Interval of parallel 5ths in the resolution of a German 6th chord
Does the Long March-11 increase its thrust after clearing the launch tower?
Which languages would be most useful in Europe at the end of the 19th century?
Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?
Has there been a multiethnic Star Trek character?
Someone whose aspirations exceed abilities or means
How did old MS-DOS games utilize various graphic cards?
Russian word for a male zebra
Check if three arrays contains the same element
Electricity free spaceship
How to search for multiple strings in a file with multiline string
grep a file, but show several surrounding lines?How to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?How do I copy a file in Python?How do I include a JavaScript file in another JavaScript file?How to do case insensitive search in VimHow do I list all files of a directory?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?How do you append to a file in Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Trying to write a script that will search an entire file for certain strings.
For more than 2 strings.
1)
First search is to check for either 1 of the following two lines:
0/RP1/CPU0 RP(Active)
Or
0/RP0/CPU0 RP(Active)
If '0/RP1/CPU0 RP(Active)' then print this message "execute command location 0/rp1/cpu0"
If '0/RP0/CPU0 RP(Active)' then print this message "execute command location 0/rp0/cpu0"
2)
Second search is to check for the either 1 of the following multi-line:
a)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : ENABLED
b)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : DISABLE
The 'TITLE_LINE(A-Z)' could differ slightly but INFO_LINE will be static and the same in either ENABLED or DISABLE.
If b) is true then print "restart process on location (FROM SEARCH1).
I have tried if/else/elif statements and have been researching using the re.search for regular expressions.
#!/usr/bin/python
activerp = open('sample-output.txt')
def check_active_rp():
for line in activerp:
if line.find('0/RP1/CPU0 RP(Active)'):
print("execute command location 0/rp1/cpu0")
else:
if line.find('0/RP0/CPU0 RP(Active)'):
print("execute command location 0/rp0/cpu0")
running this script python just returns me back to cli prompt and I couldnt get further to complete the other search.
CLI$ python test.py
CLI$
python file search
add a comment |
Trying to write a script that will search an entire file for certain strings.
For more than 2 strings.
1)
First search is to check for either 1 of the following two lines:
0/RP1/CPU0 RP(Active)
Or
0/RP0/CPU0 RP(Active)
If '0/RP1/CPU0 RP(Active)' then print this message "execute command location 0/rp1/cpu0"
If '0/RP0/CPU0 RP(Active)' then print this message "execute command location 0/rp0/cpu0"
2)
Second search is to check for the either 1 of the following multi-line:
a)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : ENABLED
b)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : DISABLE
The 'TITLE_LINE(A-Z)' could differ slightly but INFO_LINE will be static and the same in either ENABLED or DISABLE.
If b) is true then print "restart process on location (FROM SEARCH1).
I have tried if/else/elif statements and have been researching using the re.search for regular expressions.
#!/usr/bin/python
activerp = open('sample-output.txt')
def check_active_rp():
for line in activerp:
if line.find('0/RP1/CPU0 RP(Active)'):
print("execute command location 0/rp1/cpu0")
else:
if line.find('0/RP0/CPU0 RP(Active)'):
print("execute command location 0/rp0/cpu0")
running this script python just returns me back to cli prompt and I couldnt get further to complete the other search.
CLI$ python test.py
CLI$
python file search
3
you should probably useregexhere, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable
– aws_apprentice
Mar 24 at 18:42
add a comment |
Trying to write a script that will search an entire file for certain strings.
For more than 2 strings.
1)
First search is to check for either 1 of the following two lines:
0/RP1/CPU0 RP(Active)
Or
0/RP0/CPU0 RP(Active)
If '0/RP1/CPU0 RP(Active)' then print this message "execute command location 0/rp1/cpu0"
If '0/RP0/CPU0 RP(Active)' then print this message "execute command location 0/rp0/cpu0"
2)
Second search is to check for the either 1 of the following multi-line:
a)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : ENABLED
b)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : DISABLE
The 'TITLE_LINE(A-Z)' could differ slightly but INFO_LINE will be static and the same in either ENABLED or DISABLE.
If b) is true then print "restart process on location (FROM SEARCH1).
I have tried if/else/elif statements and have been researching using the re.search for regular expressions.
#!/usr/bin/python
activerp = open('sample-output.txt')
def check_active_rp():
for line in activerp:
if line.find('0/RP1/CPU0 RP(Active)'):
print("execute command location 0/rp1/cpu0")
else:
if line.find('0/RP0/CPU0 RP(Active)'):
print("execute command location 0/rp0/cpu0")
running this script python just returns me back to cli prompt and I couldnt get further to complete the other search.
CLI$ python test.py
CLI$
python file search
Trying to write a script that will search an entire file for certain strings.
For more than 2 strings.
1)
First search is to check for either 1 of the following two lines:
0/RP1/CPU0 RP(Active)
Or
0/RP0/CPU0 RP(Active)
If '0/RP1/CPU0 RP(Active)' then print this message "execute command location 0/rp1/cpu0"
If '0/RP0/CPU0 RP(Active)' then print this message "execute command location 0/rp0/cpu0"
2)
Second search is to check for the either 1 of the following multi-line:
a)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : ENABLED
b)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : DISABLE
The 'TITLE_LINE(A-Z)' could differ slightly but INFO_LINE will be static and the same in either ENABLED or DISABLE.
If b) is true then print "restart process on location (FROM SEARCH1).
I have tried if/else/elif statements and have been researching using the re.search for regular expressions.
#!/usr/bin/python
activerp = open('sample-output.txt')
def check_active_rp():
for line in activerp:
if line.find('0/RP1/CPU0 RP(Active)'):
print("execute command location 0/rp1/cpu0")
else:
if line.find('0/RP0/CPU0 RP(Active)'):
print("execute command location 0/rp0/cpu0")
running this script python just returns me back to cli prompt and I couldnt get further to complete the other search.
CLI$ python test.py
CLI$
python file search
python file search
edited Mar 24 at 18:44
Vasilis G.
4,2212925
4,2212925
asked Mar 24 at 18:40
posixposix
12
12
3
you should probably useregexhere, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable
– aws_apprentice
Mar 24 at 18:42
add a comment |
3
you should probably useregexhere, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable
– aws_apprentice
Mar 24 at 18:42
3
3
you should probably use
regex here, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable– aws_apprentice
Mar 24 at 18:42
you should probably use
regex here, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable– aws_apprentice
Mar 24 at 18:42
add a comment |
2 Answers
2
active
oldest
votes
I think this is what you want:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
add a comment |
I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.
In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here.
I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.
The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1. The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.
UPDATE:
I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the is DISABLED'.format(x.group(1)))
So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327204%2fhow-to-search-for-multiple-strings-in-a-file-with-multiline-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think this is what you want:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
add a comment |
I think this is what you want:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
add a comment |
I think this is what you want:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
I think this is what you want:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
answered Mar 24 at 19:03
WebifyWebify
5211
5211
add a comment |
add a comment |
I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.
In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here.
I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.
The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1. The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.
UPDATE:
I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the is DISABLED'.format(x.group(1)))
So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.
add a comment |
I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.
In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here.
I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.
The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1. The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.
UPDATE:
I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the is DISABLED'.format(x.group(1)))
So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.
add a comment |
I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.
In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here.
I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.
The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1. The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.
UPDATE:
I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the is DISABLED'.format(x.group(1)))
So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.
I created a file containing the strings you were searching and did some testing, and your example should have given you some output, albeit wrong. It got me thinking that you don't have a full grasp on python scripts, but correct me if I'm wrong.
In order for your function to be executed you need to call it. Writing def simply defines it. You can find more about it here.
I see you're looking at regex for this, but if there are no variations in the string you're searching for you can just use the find function.
The thing is that line.find() returns an integer and not a boolean value. So you would always enter the first if statement, unless your line started with '0/RP1/CPU0 RP(Active)' (as then it would return the 0 index). If we check the documentation we can see that find function returns -1 if no string is found. So you could change your if statement with something like this: line.find('0/RP1/CPU0 RP(Active)') != -1. The same can be done with the multiline strings. The only thing is that you need to dump the whole file in a string. So with that in mind this is the solution that could solve the problem.
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
In your example you also never close the file, so I used the with statement which is considered good practice when dealing with IO.
UPDATE:
I just figured out you would like to change what is written in info line, in that case the use of regex is appropriate. The following solution would then work:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the is DISABLED'.format(x.group(1)))
So we create an iterator out of the file and start going line by line through the file. We still use the string find function for the first string search. Now we continue on to the INFO LINE. Using the regex package we compile a regex that would capture the TITLE_LINE. Once that is found we get the next line from the iterator and once again check if the string contains ENABLED or DISABLE; and print accordingly.
edited Mar 25 at 8:58
answered Mar 25 at 8:40
MarkoMarko
487
487
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327204%2fhow-to-search-for-multiple-strings-in-a-file-with-multiline-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
you should probably use
regexhere, also you're defining a variable in the global scope but then using it within your function's local scope, that's bad practice, either pass it in as a variable or initialize it there to avoid something else possibly mutating that variable– aws_apprentice
Mar 24 at 18:42