-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-from-startline-to-endline.py
55 lines (31 loc) · 1.19 KB
/
copy-from-startline-to-endline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
# method1
with open('/path/to/input') as infile, open('/path/to/output', 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Start":
copy = True
elif line.strip() == "End":
copy = False
elif copy:
outfile.write(line)
# method2
with open('input.txt') as myfile:
content = myfile.read()
text = re.search(r'Start\n.*?End', content, re.DOTALL).group()
with open("output.txt", "w") as myfile2:
myfile2.write(text)
# method3
import itertools
with open('input.txt', 'r') as f, open('output.txt', 'w') as fout:
while True:
it = itertools.dropwhile(lambda line: line.strip() != 'Start', f)
if next(it, None) is None: break
fout.writelines(itertools.takewhile(lambda line: line.strip() != 'End', it))
# other shell perl
sed -n '/Start/,/End/p' input.txt | grep -Ev '(Start|End)'
sed -e '1,/Start/d' -e '/End/,$d' input.txt
awk /Start/,/End/ input.txt | grep -Ev '(Start|End)'
awk '/Start/{flag=1;next} /End/{flag=0} flag{ print }' input.txt
awk '/End/{flag=0} flag; /Start/{flag=1}' input.txt
perl -lne 'print if((/Start/../End/) && !(/Start/||/End/))' input.txt