I have thousands on many lines in a text file. Some consist of 2 lines, some have 1 line, as seen below:
1
00:01:18,479 --> 00:01:20,514
Dr. Oppenheimer.
2
00:01:24,052 --> 00:01:25,653
As we begin, I believe you have
3
00:01:25,686 --> 00:01:27,688
a statement to read
into the record.
4
00:01:29,523 --> 00:01:30,725
Yes, Your Honor.
5
00:01:30,758 --> 00:01:32,861
We're
not judges, Doctor.
6
00:01:32,895 --> 00:01:33,929
Of course.
......so on and on...........
I want to know how to convert a sentence that only consists of 2 lines into one line so that the final result is like this:
1
00:01:18,479 --> 00:01:20,514
Dr. Oppenheimer.
2
00:01:24,052 --> 00:01:25,653
As we begin, I believe you have
3
00:01:25,686 --> 00:01:27,688
a statement to read into the record.
4
00:01:29,523 --> 00:01:30,725
Yes, Your Honor.
5
00:01:30,758 --> 00:01:32,861
We're not judges, Doctor.
6
00:01:32,895 --> 00:01:33,929
Of course.
......so on and on...........
>Solution :
Perl to the rescue!
perl -00 -lpe '$i = 0; s/\n/(++$i > 2) ? " " : "\n"/ge' input.txt
-00turns on the "paragraph mode" which reads the file in blocks separated by more than one newline;-lremoves the final newlines from the input and adds them to output;-preads the file block by block, prints the implicit variable after running the code;s/\n/.../greplaces each newline with the...part;s///einterprets the replacement as code to evaluate;(++$i > 2) ? " " : "\n"is used to keep the first two newlines, replacing any other by a space. The variable $i serves as a line counter inside each block, it’s incremented on each newline, and once it has seen 2 of them, the ternary operator?:starts returning a space instead of a newline.