-1

I looked already at this question but nor the sed or the awk answers has helped How can I remove double line breaks with sed? .

I have a file that I get with a query from sqlplus:


FOLDER1 filename_0.sem
FOLDER1 filename_1.sem
FOLDER1 filename_2.sem
FOLDER1 filename_3.sem
FOLDER1 filename_4.sem
FOLDER1 filename_5.sem
FOLDER1 filename_6.sem
FOLDER1 filename_7.sem
FOLDER1 filename_8.sem
FOLDER1 filename_9.sem
FOLDER1 filename_10.sem
FOLDER1 filename_11.sem
FOLDER1 filename_12.sem

FOLDER1 filename_13.sem
FOLDER1 filename_14.sem
FOLDER1 filename_15.sem
FOLDER1 filename_16.sem
FOLDER1 filename_17.sem
FOLDER1 filename_18.sem
FOLDER1 filename_19.sem
FOLDER1 filename_20.sem
FOLDER1 filename_21.sem
FOLDER1 filename_22.sem
FOLDER2 filename_0.sem

24 rows selected.

I don't know and understand why it puts a lf between 12 and 13. In any case, since I can not know beforehand how many rows I get and where the double lf line is, I need to use a generic rule.

I tried these one too, but they don't work

sed -i 's/\n\n/\n//g' file.txt > otherfile.txt

or this

perl -pe 's/\n\n//' file.txt > otherfile.txt

or this

awk 1 ORS='' file.txt > otherfile.txt
EOTheDev
  • 29
  • 7
  • 1
    `sed '/^$/d'` should delete all empty lines. – M. Nejat Aydin Jul 08 '22 at 14:25
  • @M.NejatAydin thanks, with this combo `sed '/^$/d' file.txt > otherfile.txt rm file.txt mv otherfile.txt file.txt` it worked – EOTheDev Jul 08 '22 at 14:43
  • `awk NF` is even shorter. In between is: `tr -s \\n` – jhnc Jul 08 '22 at 15:58
  • 2
    Does this answer your question? [How to remove blank lines from a Unix file](https://stackoverflow.com/questions/14570489/how-to-remove-blank-lines-from-a-unix-file) – pjh Jul 08 '22 at 18:51

1 Answers1

1

If you use the command below, you can have the expected result:

grep -v "^$" file.txt > otherfile.txt

The -v parameter makes the grep to show everything that is not catch by the specified pattern. As we are specifying "^$", which ^ means the beginning of a line, and $ the end of a line, and when they are used together mean a "line with no characters", you are removing the "blank lines" with this command.