I have a file where I want to convert "\n" to " " and "\n\n" to "\n". For example:
I
had
a
toy
.
It
was
good
.
Becomes:
I had a toy .
It was good .
Does anyone have a Unix one-liner for this operation?
I have a file where I want to convert "\n" to " " and "\n\n" to "\n". For example:
I
had
a
toy
.
It
was
good
.
Becomes:
I had a toy .
It was good .
Does anyone have a Unix one-liner for this operation?
fmt | sed '/^$/d'
The fmt command will wrap lines at 75 characters, so use fmt -w [WIDTH] to set longer lines.
If you had installed Ruby ,this is the one-liner that do the trick.
irb(main):014:0> puts f.gsub(/[a-zA-Z.](\n)/) {|s| s.chomp +" "}
I had a toy .
It was good .
I think bmb's comment to his/her own answer hinted at the difficulty you may have had if you 'googled' for suggestions.
Google 'remove blank lines'.
Maybe fmt won't do it by itself, but there are many methods.
I liked the grep
approach
grep -v "^$" filename > newfilename
although it's a little messy to have to rename the file.
EDIT: one of the hits found by 'remove blank lines Perl' gave
perl -pi -e "s/^\n//" file.txt
The first thing I can think of is this:
tr '\n' '#' | sed 's/##/\n/g' | sed 's/#//g'
Where # is some character not used in the file, that's the downside of it. You'll have to use some unique character ;)
if you have gawk
# awk 'BEGIN{RS=""}{$1=$1}1' file
I had a toy .
It was good .
Yet another Awk solution:
$ cat data.txt
I
had
a
toy
.
It
was
good
.
$ awk '{printf "%s ", $0} /^$/{print ""}' data.txt
I had a toy .
It was good .