8 Replies Latest reply on Jun 7, 2019 12:10 PM by EdStevens

    Delete Empty Lines

    happy10319

      Hi,

      I have a file with empty lines.

      The content is :

      John Smith   66888:john@smith.example.fr

       

      Timn Kith: 76321:timn@kith.example.com

      PAUL KIKHTY: 54432:paul@kity.com

       

      Then I run:

      sed '/^$/d' myfile.txt

      Output is

       

      John Smith   66888:john@smith.example.fr

      Timn Kith: 76321:timn@kith.example.com

      PAUL KIKHTY: 54432:paul@kity.com

      then I run

      $ more myfile.txt

      John Smith   66888:john@smith.example.fr

       

      Timn Kith: 76321:timn@kith.example.com

      PAUL KIKHTY: 54432:paul@kity.com

       

      As you can see the empty line is not deleted. What is wrong with my sed command?

       

      Thanks.

        • 1. Re: Delete Empty Lines
          Nik

          Hi.

          Use od -c  for check contents of file.

          For example: some spaces or tabs will look like empty line, but not for sed.

          Also check how line ended (char 10 or 10 ;13) . May be it's DOS file .

           

          Regards,

             Nik

          • 2. Re: Delete Empty Lines
            happy10319

            Thanks.

            od -c myfile.txt

            0000000   J   o   h   n       S   m   i   t   h               6   6   8

            0000020   8   8   :   j   o   h   n   @   s   m   i   t   h   .   e   x

            0000040   a   m   p   l   e   .   f   r  \n  \n   T   i   m   n       K

            0000060   i   t   h   :       7   6   3   2   1   :   t   i   m   n   @

            0000100   k   i   t   h   .   e   x   a   m   p   l   e   .   c   o   m

            0000120  \n   P   A   U   L       K   I   K   H   T   Y   :       5   4

            0000140   4   3   2   :   p   a   u   l   @   k   i   t   y   .   c   o

            0000160   m  \n   J   o   h   n       S   m   i   t   h               6

            0000200   6   8   8   8   :   j   o   h   n   @   s   m   i   t   h   .

            0000220   e   x   a   m   p   l   e   .   f   r  \n   T   i   m   n

            0000240   K   i   t   h   :       7   6   3   2   1   :   t   i   m   n

            0000260   @   k   i   t   h   .   e   x   a   m   p   l   e   .   c   o

            0000300   m  \n   P   A   U   L       K   I   K   H   T   Y   :       5

            0000320   4   4   3   2   :   p   a   u   l   @   k   i   t   y   .   c

            0000340   o   m  \n

            0000343

             

            Ok. What do you see here and what is the solution?

            it is a vi file.

            Regards.

            • 3. Re: Delete Empty Lines
              Andris Perkons-Oracle

              sed does not change a file's contents unless you specify the "-i" flag.

               

              Andris

              • 4. Re: Delete Empty Lines
                happy10319

                Great.

                Thank you.

                sed -i '/^$/d' myfile.txt

                $ more myfile.txt

                John Smith  66888:john@smith.example.fr

                Timn Kith: 76321:timn@kith.example.com

                PAUL KIKHTY: 54432:paul@kity.com

                • 5. Re: Delete Empty Lines
                  EdStevens

                  happy10319 wrote:

                   

                  Thanks.

                  od -c annuaire.txt

                  0000000 J o h n S m i t h 6 6 8

                  0000020 8 8 : j o h n @ s m i t h . e x

                  0000040 a m p l e . f r \n \n T i m n K

                  0000060 i t h : 7 6 3 2 1 : t i m n @

                  0000100 k i t h . e x a m p l e . c o m

                  0000120 \n P A U L K I K H T Y : 5 4

                  0000140 4 3 2 : p a u l @ k i t y . c o

                  0000160 m \n J o h n S m i t h 6

                  0000200 6 8 8 8 : j o h n @ s m i t h .

                  0000220 e x a m p l e . f r \n T i m n

                  0000240 K i t h : 7 6 3 2 1 : t i m n

                  0000260 @ k i t h . e x a m p l e . c o

                  0000300 m \n P A U L K I K H T Y : 5

                  0000320 4 4 3 2 : p a u l @ k i t y . c

                  0000340 o m \n

                  0000343

                   

                  Ok. What do you see here and what is the solution?

                  it is a vi file.

                  Regards.

                  There's no such thing as "a vi file".  vi is just a text editor.  The *nix version of notepad.  Would you refer to a file on Windows as "a notepad file"? 

                   

                  What you show above does not really give what we need to see.  Open your file in vi, then put vi into 'hex mode'.  I leave it as an exercise for the student to research how to put vi into hex mode.

                  • 6. Re: Delete Empty Lines
                    Dude!

                    A command to check line endings (and other gremlins) and that is also easy to remember is

                     

                          cat -vet  filename

                     

                    For example:

                     

                    [dude@localhost ~]$ cat -vet testfile

                    some stuff$

                    another line$

                    $

                    after an empty line$

                      $

                    after an empty line with spaces.$

                    ^I$

                    after an empty line with a tab.$

                    In the above example you can see ^I as tab and $ as newline.

                     

                    To remove all "empty" lines using sed and inline editing:

                     

                    [dude@localhost ~]$ sed -i '/^[[:space:]]*$/d' testfile

                     

                    [dude@localhost ~]$ cat -vet testfile

                    some stuff$

                    another line$

                    after an empty line$

                    after an empty line with spaces.$

                    after an empty line with a tab.$

                    • 7. Re: Delete Empty Lines
                      Gaz in Oz

                      EdStevens wrote:

                      ...

                      vi is just a text editor.  The *nix version of notepad.

                      ...

                      I strongly disagree with "The *nix version of notepad.".

                      • 8. Re: Delete Empty Lines
                        EdStevens

                        Gaz in Oz wrote:

                         

                        EdStevens wrote:

                        ...

                        vi is just a text editor. The *nix version of notepad.

                        ...

                        I strongly disagree with "The *nix version of notepad.".

                        Why would you disagree? 

                        It's true that vi is far more powerful than notepad, but they are both 'just' text editors.