Search code examples
bashshellsedawkash

how to replace 2 new lines by one in the file


I have the following text file

config 'toto'
        option 
        option 

config 'titi'
        list 
        list 

config 'tutu'
        list 
        list 

I want to replace each 2 new lines by only one when I display the file with cat.

I tried the following commands but they didn't work

cat file | sed -e "s@$'\n'$'\n'@$'\n'@g"
cat file | sed -e "s@\n\n@\n@g"

the expected output is like this :

config 'toto'
        option 
        option 
config 'titi'
        list 
        list 
config 'tutu'
        list 
        list 

Solution

  • With sed:

    sed '/^$/d' file
    

    (OR)

    sed '/^[ ]*$/d' file
    

    With tr:

    tr -s '\n' < file