Remove empty lines in a txt file

Rom1 -  
 cenedrill -
Hi folks!!

So, I have a text file that has a lot of empty lines that I would like to remove. What command would be suitable for this situation??

Also, I would like to add the same number at the end of each line, for example:

jsdfj djfdjf 8
jdjfj daeradf 8
hgag gsdok, 8

What can I do??

Thanks a lot to those who will respond!

10 answers

  1. asevere Posted messages 13095 Registration date   Status Webmaster Last intervention   426
     
    Hi,

    There are plenty of methods, the sed command seems appropriate to me :)

    Depending on the version, you might even be able to avoid a temporary file to do this. (-i)

    Removing empty lines:
    sed '/^$/d' test >test.tmp mv test.tmp test or sed -i '/^$/d' test
    (d) deletes the line that matches the pattern(/^$/)
    ^ represents the start of the line
    $ represents the end of the line
    ^$ thus represents an empty line.

    To add a number at the end of each line:
    sed 's/\(.*\)/\1 ton_chiffre/' test >test.tmp mv test.tmp test or sed -i 's/\(.*\)/\1 ton_chiffre/' test
    \1 represents what is in the first pair of parentheses.
    You substitute (s) the line(.*) with the line (\1) plus your number.

    You can then combine both:
    sed -e '/^$/d' -e 's/\(.*\)/\1 ton_chiffre/' test >test.tmp or sed -i -e '/^$/d' -e 's/\(.*\)/\1 ton_chiffre/' test
    There you go :)

    ++
    --
    A weasel, a rabbit!?
    That's not normal! :p
    21
    1. cenedrill
       
      Simple but always useful, thank you ^_^
      0
  2. tomtomjajah Posted messages 1 Status Member 9
     
    To remove empty lines, it's better to use this:

    sed -i "/^[ \t]*$/d" filename

    This command deletes all lines that contain only spaces or tabs or ... nothing at all, because there is a difference between nothing, a space, and a tab. That's probably why it wasn't working for rom1.
    Alright, it was in 2005, but it will be useful for others.
    9
    1. asevere Posted messages 13095 Registration date   Status Webmaster Last intervention   426
       
      In that case, \s* is enough :)

      But you are right, there is a difference between an empty line and a line that only contains spaces, tabs, etc... so to remove "empty" lines it is better to use "^$" ;-)

      See you later
      0
  3. asevere Posted messages 13095 Registration date   Status Webmaster Last intervention   426
     
    re :)

    So in that case, it might be that your lines are not empty,
    to check this, a simple
    egrep "^$" yourfile | wc -l
    will answer you very quickly. (search for empty lines and count) if the result is zero, several leads to follow:
    - DOS format file
    - space(s)
    - tab(s)

    etc.
    --
    A weasel, a rabbit!?
    That's not normal! :p
    1
  4. Rom1
     
    Wow... that looks quite impressive ;) Well, I'll test it out and keep you posted

    Thanks anyway!!
    0
  5. Rom1
     
    Cool, the command to add a number at the end of each line works like a charm, but the one to delete empty lines doesn’t give me any error messages but doesn’t work, which is weird, it seems coherent though...
    0
  6. Rom1
     
    What insight, indeed... this command did indeed return 0; there were surely some spaces in those infamous empty lines.

    Thank you very much, in any case!!
    0
  7. leburgeois8 Posted messages 47 Status Member 14
     
    hello everyone
    I read the previous posts and they are great, but I have a problem.

    Actually, I’m looking for how to add a line to a file (which is already resolved) but the issue is searching in that file and being able to retrieve that line to delete it when necessary.

    Let me explain, let’s suppose I have a list of people with their IDs and phone numbers in my file and these people subscribe to a service for a specific period. When the subscription period comes to an end, I trigger an update that goes into the file and searches for the IDs of those concerned individuals and deletes the corresponding lines associated with their IDs and phone numbers.

    Thanks in advance!!
    0
  8. asevere Posted messages 13095 Registration date   Status Webmaster Last intervention   426
     
    Hello,

    In post 1, you should have the answer (or at least all the elements to get it)

    It would be good to include an example of the file in question.

    You know the identifier and you want to delete the corresponding lines?
    Or do you need to find an identifier based on a date, and then delete it?

    In the example of the file, try to include as many cases as possible:
    All existing line types, records to delete, records to keep, and of course, include fake numbers!!!

    If possible, based on the example, show us what the file should look like after modifications :)

    See you soon
    --

    A weasel, a rabbit!?
    That's not normal!
    -1