Search code examples
linuxbash

Print multiple lines as single line if they fit a pattern


I have a file with multiple lines and I want to join lines only if they fit a specific pattern. I know that I can join the lines with, for example:

$ cat input.txt
1
00:02:34,654 --> 00:02:36,554
Sean, let's go.

2
00:02:38,358 --> 00:02:40,724
Yeah. Detective. we got
a possible 1 87 out on Route 59.

3
00:03:04,584 --> 00:03:06,051
Right this way.

4
00:05:01,301 --> 00:05:03,269
How long has this been here?./
Tuesday.
$ cat input.txt | perl -00 -lpe '$i = 0; s/\n/(++$i > 2) ? " " : "\n"/ge'
1
00:02:34,654 --> 00:02:36,554
Sean, let's go.

2
00:02:38,358 --> 00:02:40,724
Yeah. Detective. we got a possible 1 87 out on Route 59.

3
00:03:04,584 --> 00:03:06,051
Right this way.

4
00:05:01,301 --> 00:05:03,269
How long has this been here?./ Tuesday.

But, how can I solve if the line fits the pattern (only if they have a '/') ?. So in this case, the output I would like to achieve would be:

1
00:02:34,654 --> 00:02:36,554
Sean, let's go.

2
00:02:38,358 --> 00:02:40,724
Yeah. Detective. we got
a possible 1 87 out on Route 59.

3
00:03:04,584 --> 00:03:06,051
Right this way.

4
00:05:01,301 --> 00:05:03,269
How long has this been here?./ Tuesday.

Solution

  • sed '/\//{N;s/\n/ /;}'
    
    • /\// if the line contains /
    • N add newline and next line
    • s/\n/ / replcae newline wiht a spce.