Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash: Commands to Convert Multi Lines to Single Line

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...........

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Perl to the rescue!

perl -00 -lpe '$i = 0; s/\n/(++$i > 2) ? " " : "\n"/ge' input.txt
  • -00 turns on the "paragraph mode" which reads the file in blocks separated by more than one newline;
  • -l removes the final newlines from the input and adds them to output;
  • -p reads the file block by block, prints the implicit variable after running the code;
  • s/\n/.../g replaces each newline with the ... part;
  • s///e interprets 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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading