I have a file like this note gap represent new lines.
Hello World )
;
Hello World ) ;
Hello World )
;
I have written a small python script to move the semicolon to the end of previous line
with open(path) as f:
prev_line =''
for current_line in f:
matched = re.match('[^(.+)];',current_line,re.MULTILINE)
if matched is not None:
current_line = re.sub('[^(.+)];','',current_line,re.MULTILINE)
prev_line = re.sub(r'^(.+)$',r'\1 ;',prev_line,re.MULTILINE)
print prev_line.strip()
prev_line = current_line.strip()
I am getting the expected out put except for the last line where the semicolon is missing
Hello World ) ;
Hello World ) ;
Hello World ) **semicolon is missing here**
You should try using
\s*:\s*;matches any amount of whitespace (including newlines) followed by a semicolon.Also,
re.sub()works on any number of instances of that pattern, so you could just do something like this: