Skip to main content
3 of 4
added 4 characters in body
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

In perl

perl -n00E 'chomp;s/\n/ /g;say' your_file

Explanation

  • -n: Operate on the file record by record
  • -00: Means the record separator is two or more consecutive newlines
  • -E : execute the following string as code while setting the default variable ($_) to the record currently being read from the file.
  • chomp: remove the record separator from $_
  • s/\n/ /g: Replace every newline encountered in the current record with a space (the g modifier ensures the replacement is "global").
  • say: Prints the contents of $_ with a newline added at the end. It is used here instead of print because chomp will have removed the newlines separating the different paragraphs. It is equivalent to print "$_\n".
  • We needed to use -E rather than -e because it enables the use of more recent language features; the say function in this case.
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146