Reputation: 3234
I'm looking for way to remove every lines before line which contains specific string in multiline string like this:
string1
string2
string3
==== bump
string4
string5
string6
==== bump
But only first matching one...
At the end I would like to have this as an output:
==== bump
string4
string5
string6
==== bump
Upvotes: 1
Views: 101
Reputation: 84343
Assuming that you've stored your text in /tmp/corpus, you could use the following Perl one-liner:
perl -ne 'print if /\A==== bump/ ... /\A==== bump/' /tmp/corpus
This leverages the power of Perl's range operator. If you want to capture the output from Perl within your Python program, you can use the Python subprocess module. For example:
import subprocess
result = subprocess.check_output(
"perl -ne 'print if /\A==== bump/ ... /\A==== bump/' /tmp/corpus",
shell=True)
print result
Upvotes: 1
Reputation: 369064
lines = '''
string1
string2
string3
==== bump
string4
string5
string6
==== bump
'''
import re
sep = '==== bump'
matched = re.search('{0}.*?{0}'.format(re.escape(sep)), lines, flags=re.S)
print(matched.group(0))
Upvotes: 0
Reputation: 369064
import io
import itertools
import sys
lines = io.StringIO(u'''\
string1
string2
string3
==== bump
string4
string5
string6
==== bump
''')
sep = '==== bump'
it = itertools.dropwhile(lambda line: not line.startswith(sep), lines)
sys.stdout.writelines(it)
Output
==== bump
string4
string5
string6
==== bump
Upvotes: 1
Reputation: 879451
import re
text = '''\
string1
string2
string3
==== bump
string4
string5
string6
==== bump'''
print(re.split(r'(=== bump)', text, maxsplit=1)[-1])
yields
string4
string5
string6
==== bump
Upvotes: 1