I have a text file many thousands of lines long with ASCII and non-ACII characters. It is supposed to follow a pattern of
First line: only non-ASCII characters
Second line: only non-ASCII characters
Third line: only ASCII characters
Fourth line: mix of ASCII and non-ASCII characters
Unfortunately, the reality is that it looks something like the following example, where in the middle it is missing the line that mixes ASCII and non-ASCII characters:
日本語のみ
日本語のみ
English words only
English and 日本語
日本語のみ
日本語のみ
English words only
日本語のみ
日本語のみ
English words only
English and 日本語
Fortunately, as far as I can tell, it is only the line that mixes ASCII and non-ASCII characters that is sometimes absent. Meaning that what should be groups of 4 lines are sometimes groups of only 3.
To fix the file, I need to:
- Search for every line with only ASCII characters.
- Test the line following to see if it contains only non-ASCII.
- If so, insert a placeholder line following the ASCII only line.
The result should be:
日本語のみ
日本語のみ
English words only
English and 日本語
日本語のみ
日本語のみ
English words only
+Aあ+
日本語のみ
日本語のみ
English words only
English and 日本語
(I chose to make the placeholder +Aあ+
so that it will conform to the mix of ASCII and non-ASCII as the lines it is standing in for.)
I've found I can use sed to insert new lines sed -e "/this is existing text/a\'this is a new line'" < file.text. And I've learned I can search for ASCII characters with sed using LC_ALL=C
and [\d0-\d127]
.
However, I'm unclear on how to make a conditional separate from the search. I mean, I could insert a line after every instance of ASCII only characters, but how do I make a search that inserts a line when an all ASCII line is found and the next line is only non-ASCII?
Please note that I am not particular about using sed. If an answer can be provided using Gedit, LibreOffice, or any command line operation, that would be great.