INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Remove empty lines

Remove empty lines

(OP)
Hi
I am trying to remove empty lines in my file using:
sed '/^$/d' file.txt

However, nothing seems to be removed. Can you help me here please :)

my file looks like this:

3621    PSMD5    NM_005047
23626    NELL2    NM_006159
        
23633    
23643    CAPN2    NM_001748
    
23661    PIAS4    NM_015897
23684    STMN1    NM_005563

Thanks.

curious1

RE: Remove empty lines

Those lines aren't empty, they have spaces on them.

Try sed '/^ *$/d' file.txt.

Annihilannic.

RE: Remove empty lines

Another way:
awk NF file.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886

RE: Remove empty lines

(OP)
Hi Annihannic
Thanks for replying.
I tried your command but it did not change anything in my file either. Any alternatives :)

curious1

RE: Remove empty lines

(OP)
Hi PHV
Your suggestions did not work either. Is it because I am using nawk on Sun OS?

curious1

RE: Remove empty lines

Seems you have some control char in your file, perhaps CR (\r) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886

RE: Remove empty lines

try an "od -xc" on the same excerpt of the file and post the output back here, perhaps there are only tabs in the file?

HTH,

p5wizard

RE: Remove empty lines

Are you expecting these commands to change the original file?  If so, they don't.  You will need to redirect the output to a new file.

sed '/^ *$/d' file.txt > file2.txt

If you knew that already, ignore me.  smile

Annihilannic.

RE: Remove empty lines

(OP)
Hi P5wizard,
May I ask what you mean " by adding "od -xc" on the same excerpt of the file and post the output back here"? Sorry, I am still a green beginner in this area :)
Maybe I explain a little about my files.
1)I extracted data from certain columns in Excel tables and saved it as tab-delimited file.
2)After that, I compared 2 files and extract those present in both files.
3)the result I obtained contain many blank/empty lines which I now want to remove using the sed command.

I did not manage to do this with the sed command I mentioned earlier.

Hope you can help.

curious1

RE: Remove empty lines

Try

sed '/^[ (tab)]*$/d' file.txt > file2.txt

However where it says "(tab)" just hit the tab key to insert a tab character.

So it should look like this:

sed '/^[     ]*$/d' file.txt > file2.txt

Annihilannic.

RE: Remove empty lines

you have a piece of your datafile in another file?

od -xc /path/to/piece

you created that excerpt by head or tail?

tail -10 /path/to/file|od -xc

paste the output of the od -xc back here please

HTH,

p5wizard

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close