5

Is it possible to replace only double newlines in a text file using sed, awk, grep, tr, or whatever is needed?

this

is

data
this

is

more

data

What I need to do is to replace all double newlines (\n\n, there is no whitespace) with a space, but keep all single newlines, as to have all my data points on their own line.

this is data
this is more data

Is this possible?

3 Answers 3

8

Try

    sed ':a;N;$!ba;s/\n\n/ /g'  filename

This will first read the whole file, then replace the double newlines (and only those!) with a space. This trick (reading the whole file) is necessary because most GNU/Linux utilities process input one line at a time, which would give you the wrong result.

1
  • This command doesn't work for me. sed ':a;N;$!ba;s/\n\n/ /g' file1.txt > file2.txt results in both files having the exact same MD5 hash. Commented Dec 9, 2018 at 18:46
6

Just another variant: an awk version

awk 'BEGIN{RS="\n\n" ; ORS=" ";}{ print }' 
1
  • 2
    Note: RS is the intput record separator, by default a newline.ORS is the output record separator, by default a newline. As usual man awk to get more information.
    – Hastur
    Commented Oct 6, 2015 at 12:03
2

Adding to @MariusMatutiae's answer: If the file is too big to read to memory, you can use a classic but much slower way:

EMPTY=0
BUFFER=""
while read L; do
  if test -z "$L"; then
    EMPTY=$(($EMPTY+1))
    BUFFER="$BUFFER\n"
  else
    if test $EMPTY -lt 2; then
      echo -en "$BUFFER"
    else
      echo " "
    fi
    echo "$L"
    BUFFER=""
    EMPTY=0
  fi
done < filename
echo -ne "$BUFFER"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .