I have text of a poem that starts with the original language on odd numbered lines and a translation on even numbered lines. I want to remove the original language odd lines. I could use the Notepad replace function with regex ([^\n]*\n)[^\n]*\n
to remove every odd numbered line but when it reaches the blank lines between paragraphs, it switches to removing the translated lines and some of the blank lines also get removed.
How would I remove the odd lines while skipping and keeping the blank lines (it doesn't have to be done in Notepad )?
CodePudding user response:
One option would be:
^. \R(. )
See an online demo
^
- Start-line anchor;. \R
- Any 1 characters upto any unicode newlince sequence;(. )
- Capture a 2nd line of 1 characters in a capture group.
Replace with \1
CodePudding user response:
You can do it with a small python script like this
lines = []
with open("sample.txt", "r") as f:
for line in f:
if len(line) > 1:
lines.append(line)
with open("sample.txt", "w") as f:
for number, line in enumerate(lines):
if not number % 2 == 0:
f.write(line)
It checks if the line length is > 1 because newline counts as 1 for the length. It then appends the line to the list lines
if there is any text in the line. It then checks if the line number is even or odd with if not number & 2 == 0
(python indexing starts at 0 so 1 would actually be an even number. Then it overrides the file with only even lines. If you want to delete even lines and keep odd lines, just remove the not
in second to last line.