Find the answer to your Linux question:
Results 1 to 7 of 7
I have a list of web addresses in a text file on my Linux server. Users can edit that file of addresses across the network from Windows machines. I have ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10

    [SOLVED] Deleting hard returns problem



    I have a list of web addresses in a text file on my Linux server. Users can edit that file of addresses across the network from Windows machines. I have some scripts to analyze the file and I need to remove empty lines, hard returns. Such as:

    lenovo.com

    dell.com
    hp.com


    Becoming:

    lenovo.com
    dell.com
    hp.com


    If the file is created and edited ONLY in Linux, this has always worked fine:
    Code:
    grep -iv "^$" /path/to/file

    However, if the file has been edited by a Windows machine somewhere on the network, then the emtpy lines aren't removed. I've done some searching and tried this line:
    Code:
    perl -pi -e 'tr/[\012\015]//d' >
    But it works a little too well. It strips line returns and hard returns so that the output is
    Code:
    lenovo.com.dell.com.hp.com
    When I need to have
    lenovo.com
    dell.com
    hp.com

    Anybody have the answer for me? I've also read about the dos2unix command but that's not an option to use in this case.

    Thank you for suggestions.

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,043
    Wouldn't
    Code:
    perl -pi -e 'tr/[\015]//d'
    do the trick?

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10
    For some frustrating reason, it doesn't. I had tried using that line yesterday and didn't mention it in my first post.

    But this snippet doesn't take any lines out at all.
    Code:
    perl -pi -e 'tr/[\015]//d'
    Output remains

    lenovo.com

    dell.com
    hp.com

    Using this line:
    Code:
    perl -pi -e 'tr/[\012]//d'
    Output is:

    lenovo.comdell.comhp.com

    Sorta drivin' me nuts.

    Thank you for your reply.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Two questions.
    I've also read about the dos2unix command but that's not an option to use in this case.
    Why not?

    And ...

    Would you be so kind as to run the following command against your input file, and copy and paste the output to this discussion thread?:
    Code:
    od -t x1 filename
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10
    Why dos2unix isn't an option...well, that one takes a little too long to explain.

    Output from od -t x1 is

    Code:
    0000000 23 43 6f 6d 70 75 74 65 72 20 68 61 72 64 77 61
    0000020 72 65 20 2f 20 44 72 69 76 65 72 73 0d 0a 2e 68
    0000040 70 2e 63 6f 6d 0d 0a 2e 68 70 2d 77 77 2e 63 6f
    0000060 6d 0d 0a 2e 74 6f 73 68 69 62 61 2e 63 6f 6d 0d
    0000100 0a 2e 64 65 6c 6c 2e 63 6f 6d 0d 0a 2e 64 65 6c
    0000120 6c 6f 75 74 6c 65 74 2e 63 6f 6d 0d 0a 2e 61 63
    0000140 65 72 2e 63 6f 6d 0d 0a 2e 6c 65 6e 6f 76 6f 2e
    0000160 63 6f 6d 0d 0a 2e 69 6e 74 65 6c 2e 63 6f 6d 0d
    0000200 0a 2e 6e 76 69 64 69 61 2e 63 6f 6d 0d 0a 2e 61
    0000220 6d 64 2e 63 6f 6d 0d 0a 2e 61 74 69 2e 63 6f 6d
    0000240 0d 0a 2e 74 6f 73 68 69 62 61 64 69 72 65 63 74
    0000260 2e 63 6f 6d 0d 0a 2e 74 6f 73 68 69 62 61 70 63
    0000300 2e 63 6f 6d 0d 0a 2e 74 6f 73 68 69 62 61 2e 63
    0000320 6f 6d 0d 0a 2e 6c 6f 67 69 74 65 63 68 2e 63 6f
    0000340 6d 0d 0a 0d 0a 23 50 72 69 6e 74 65 72 73 20 26
    0000360 20 53 63 61 6e 6e 65 72 73 20 2f 20 44 72 69 76
    0000400 65 72 73 0d 0a 2e 65 70 73 6f 6e 2e 63 6f 6d 0d
    0000420 0a 2e 6c 65 78 6d 61 72 6b 2e 63 6f 6d 0d 0a 2e
    0000440 62 72 6f 74 68 65 72 2e 63 6f 6d 0d 0a 2e 62 72
    0000460 6f 74 68 65 72 2d 75 73 61 2e 63 6f 6d 0d 0a 2e
    0000500 76 69 73 69 6f 6e 65 65 72 2e 63 6f 6d 0d 0a 2e
    0000520 6b 6f 6e 69 63 61 6d 69 6e 6f 6c 74 61 2e 63 6f
    0000540 6d 0d 0a 2e 6f 6b 69 2e 63 6f 6d 0d 0a 2e 6f 6b
    0000560 69 64 61 74 61 2e 63 6f 6d 0d 0a 2e 78 65 72 6f
    0000600 78 2e 63 6f 6d 0d 0a 0d 0a 23 53 6f 66 74 77 61
    0000620 72 65 0d 0a 2e 6d 69 63 72 6f 73 6f 66 74 2e 63
    0000640 6f 6d 0d 0a 2e 77 69 6e 64 6f 77 73 75 70 64 61
    0000660 74 65 2e 63 6f 6d 0d 0a 2e 6d 73 64 6e 2e 63 6f
    0000700 6d 0d 0a 2e 61 70 70 6c 65 2e 63 6f 6d 0d 0a 2e
    0000720 61 64 6f 62 65 2e 63 6f 6d 0d 0a 2e 6a 61 76 61
    0000740 2e 63 6f 6d 0d 0a 2e 73 75 6e 2e 63 6f 6d 0d 0a
    0000760 2e 6c 6f 74 75 73 2e 63 6f 6d 0d 0a 2e 61 63 74
    0001000 2e 63 6f 6d 0d 0a 61 32 34 38 2e 65 2e 61 6b 61
    0001020 6d 61 69 2e 6e 65 74 0d 0a 2e 64 69 67 69 74 61
    0001040 6c 72 69 76 65 72 2e 63 6f 6d 0d 0a 0d 0a 23 42
    0001060 72 6f 77 73 65 72 20 73 6f 66 74 77 61 72 65 0d
    0001100 0a 2e 6f 70 65 72 61 2e 63 6f 6d 0d 0a 2e 6d 6f
    0001120 7a 69 6c 6c 61 2e 63 6f 6d 0d 0a 2e 66 69 72 65
    0001140 66 6f 78 2e 63 6f 6d 0d 0a 0d 0a 23 53 65 63 75
    0001160 72 69 74 79 20 53 6f 66 74 77 61 72 65 0d 0a 2e
    0001200 73 79 6d 61 6e 74 65 63 2e 63 6f 6d 0d 0a 2e 73
    0001220 79 6d 61 6e 74 65 63 73 74 6f 72 65 2e 63 6f 6d
    0001240 0d 0a 2e 73 79 6d 61 6e 74 65 63 6c 69 76 65 75
    0001260 70 64 61 74 65 2e 63 6f 6d 0d 0a 2e 73 70 79 77
    0001300 61 72 65 62 6f 74 2e 63 6f 6d 0d 0a 2e 69 6e 73
    0001320 74 61 6c 6c 73 68 69 65 6c 64 2e 63 6f 6d 0d 0a
    0001340 2e 67 72 69 73 6f 66 74 2e 63 7a 0d 0a 2e 61 76
    0001360 67 2e 63 6f 6d 0d 0a 2e 6d 63 61 66 65 65 2e 63
    0001400 6f 6d 0d 0a 2e 6c 61 76 61 73 6f 66 74 75 73 61
    0001420 2e 63 6f 6d 0d 0a 2e 6c 61 76 61 73 6f 66 74 2e
    0001440 64 65 0d 0a 2e 6c 61 76 61 73 6f 66 74 2e 63 6f
    0001460 6d 0d 0a 2e 63 6f 6d 6f 64 6f 2e 63 6f 6d 0d 0a
    0001500 2e 7a 6f 6e 65 61 6c 61 72 6d 2e 63 6f 6d 0d 0a
    0001520 2e 7a 6f 6e 65 6c 61 62 73 2e 63 6f 6d 0d 0a 2e
    0001540 6b 61 73 70 65 72 73 6b 79 2e 63 6f 6d 0d 0a 2e
    0001560 69 6f 70 75 73 2e 63 6f 6d 0d 0a 2e 61 67 6e 69
    0001600 74 75 6d 2e 63 6f 6d 0d 0a 2e 73 75 6e 62 65 6c
    0001620 74 73 6f 66 74 77 61 72 65 2e 63 6f 6d 0d 0a 0d
    0001640 0a 23 52 6f 75 74 65 72 73 20 44 72 69 76 65 72
    0001660 73 20 26 20 54 65 63 68 20 73 75 70 70 6f 72 74
    0001700 0d 0a 2e 7a 79 78 65 6c 2e 63 6f 6d 0d 0a 2e 32
    0001720 77 69 72 65 2e 63 6f 6d 0d 0a 2e 6c 69 6e 6b 73
    0001740 79 73 2e 63 6f 6d 0d 0a 2e 6e 65 74 67 65 61 72
    0001760 2e 63 6f 6d 0d 0a 2e 63 69 73 63 6f 2e 63 6f 6d
    0002000 0d 0a 2e 64 6c 69 6e 6b 2e 63 6f 6d 0d 0a 0d 0a
    0002020 23 43 44 20 62 75 72 6e 65 72 73 0d 0a 2e 6e 65
    0002040 72 6f 2e 63 6f 6d 0d 0a 2e 72 6f 78 69 6f 2e 63
    0002060 6f 6d 0d 0a 0d 0a 23 41 63 63 6f 75 6e 74 69 6e
    0002100 67 20 53 6f 66 74 77 61 72 65 0d 0a 2e 71 75 69
    0002120 63 6b 62 6f 6f 6b 73 2e 63 6f 6d 0d 0a 2e 69 6e
    0002140 74 75 69 74 2e 63 6f 6d 0d 0a 2e 71 75 69 63 6b
    0002160 65 6e 2e 63 6f 6d 0d 0a 2e 74 75 72 62 6f 74 61
    0002200 78 2e 63 6f 6d 0d 0a 2e 74 61 78 63 75 74 2e 63
    0002220 6f 6d 0d 0a 2e 74 61 78 61 63 74 2e 63 6f 6d 0d
    0002240 0a 2e 66 69 6e 64 6d 79 6f 72 64 65 72 2e 63 6f
    0002260 6d 0d 0a 2e 70 65 61 63 68 74 72 65 65 2e 63 6f
    0002300 6d 0d 0a 2e 73 61 67 65 73 6f 66 74 77 61 72 65
    0002320 2e 63 6f 6d 0d 0a 0d 0a 23 53 68 69 70 70 65 72
    0002340 73 0d 0a 2e 73 74 61 6d 70 73 2e 63 6f 6d 0d 0a
    0002360 2e 70 68 6f 74 6f 73 74 61 6d 70 73 2e 63 6f 6d
    0002400 0d 0a 2e 66 65 64 65 78 2e 63 6f 6d 0d 0a 2e 75
    0002420 70 73 2e 63 6f 6d 0d 0a 2e 75 70 73 66 72 65 69
    0002440 67 68 74 2e 63 6f 6d 0d 0a 2e 75 73 70 73 2e 63
    0002460 6f 6d 0d 0a 2e 64 68 6c 2e 63 6f 6d 0d 0a 2e 64
    0002500 68 6c 2d 75 73 61 2e 63 6f 6d 0d 0a 0d 0a 23 53
    0002520 65 61 72 63 68 20 65 6e 67 69 6e 65 0d 0a 2e 67
    0002540 6f 6f 67 6c 65 2e 63 6f 6d 0d 0a 0d 0a 2e 64 61
    0002560 69 6c 79 68 6f 77 6c 65 72 73 2e 63 6f 6d 0d 0a
    0002600 2e 66 75 6e 6e 79 6a 6f 6b 65 73 2e 63 6f 6d 0d
    0002620 0a 0d 0a 23 47 65 6e 65 72 61 6c 0d 0a 2e 61 75
    0002640 74 68 6f 72 69 7a 65 2e 6e 65 74 0d 0a 2e 6d 61
    0002660 70 71 75 65 73 74 2e 63 6f 6d 0d 0a 2e 64 6f 77
    0002700 6e 6c 6f 61 64 2e 63 6f 6d 0d 0a 2e 73 6f 66 74
    0002720 70 65 64 69 61 2e 63 6f 6d 0d 0a 0d 0a 23 50 68
    0002740 6f 6e 65 62 6f 6f 6b 0d 0a 2e 77 68 69 74 65 70
    0002760 61 67 65 73 2e 63 6f 6d 0d 0a 2e 79 65 6c 6c 6f
    0003000 77 2e 63 6f 6d 0d 0a 2e 79 65 6c 6c 6f 77 70 61
    0003020 67 65 73 2e 63 6f 6d 0d 0a 2e 61 64 64 72 65 73
    0003040 73 65 73 2e 63 6f 6d 0d 0a 0d 0a 23 53 74 61 74
    0003060 65 73 0d 0a 73 74 61 74 65 2e 6e 6a 2e 75 73 0d
    0003100 0a 73 74 61 74 65 2e 70 61 2e 75 73 0d 0a 73 74
    0003120 61 74 65 2e 76 74 2e 75 73 0d 0a 6d 6f 2e 67 6f
    0003140 76 0d 0a 69 6c 6c 69 6e 6f 69 73 2e 67 6f 76 0d
    0003160 0a 69 72 73 2e 67 6f 76 0d 0a 0d 0a 23 45 6d 61
    0003200 69 6c 20 0d 0a 2e 6a 75 6e 6f 2e 63 6f 6d 0d 0a
    0003220 2e 65 6d 79 70 65 6f 70 6c 65 2e 6e 65 74 0d 0a
    0003240 2e 65 6d 79 70 65 6f 70 6c 65 2e 63 6f 6d 0d 0a
    0003260 2e 61 66 6f 2e 6e 65 74 0d 0a 2e 6d 79 61 66 6f
    0003300 2e 6e 65 74 0d 0a
    0003306

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Why dos2unix isn't an option...well, that one takes a little too long to explain.
    Well, we might need the explanation in order to figure out what will work.

    My machine doesn't have dos2unix, but it does have fromdos, which does the same thing.

    I took your file, reconstructed it bit for bit on my Linux machine, and this worked:
    Code:
    fromdos < z.txt | grep -v '^$' > z1.txt
    If dos2unix won't work for you, and fromdos won't work for you, and perl does work for you, try this:
    Code:
    perl -pe 's/\015//' < z.txt | grep -v '^$' > z1.txt
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10
    Thanks everyone for your input on this thread. Some answers here helped me know more what to look for and what to search for.

    Here's the line that's doing the trick for me - the domainlist.txt file is the file that's editable by various Windows computers, which are then inserting the difficult-to-delete, non-Linux friendly hard returns.
    Code:
    cat domainlist.txt | tr -d "\r" > domainlist2.tmp
    Then use this command and presto all commands and empty spaces are removed just like I need.

    Code:
    grep -iv "^#" domainlist2.tmp | grep -iv "^$" > domainlist3.tmp
    Again, thanks to everyone for their help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •