Visit Our UNIX and Linux User Community


Remove from a file all lines with value equal to 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove from a file all lines with value equal to 0
# 1  
Old 09-06-2013
Remove from a file all lines with value equal to 0

Dear All,
I have a file containing 1134 columns and 20825 rows,
tabulated as follow

Code:
	-4000	-3900	-3800	-3700	-3600	-3500	-3400
NR_033530	0	0	0	0	0	0	0
NM_001162375	0	0	0	0	0	0	0
NM_007669	0	0	0	0	0	0	328,98
NM_008104	0	388,94	388,94	388,94	0	0	0
NM_010472	0	0	0	0	0	0	0
NM_001110205	0	0	0	0	0	0	0
NM_001110204	0	0	0	0	98,76	0	0
NM_001110193	0	0	0	0	0	0	0
NM_019747	0	0	0	0	0	0	0
NM_021338	328,98	0	0	0	0	0	0
NR_029560	0	0	0	0	0	0	0
NR_004853	0	0	0	0	388,94	388,94	388,94
NM_008164	0	0	0	0	0	0	0
NM_008162	0	0	0	0	0	0	0
NR_027664	0	0	0	0	0	0	0
NM_175091	0	0	0	0	0	0	0
NM_173770	0	388,94	388,94	388,94	0	0	0
NM_178045	0	0	0	0	0	0	0
NM_178028	0	0	0	0	0	0	0
NM_177909	0	0	98,76	0	0	0	0
NM_177638	0	0	0	0	0	0	0
NM_177633	0	328,98	0	0	0	0	0
NM_178757	0	0	0	0	2	0	0


I would like to remove all lines with all values equal to 0, to achieve a file like this:

Code:
	-4000	-3900	-3800	-3700	-3600	-3500	-3400
NM_021338	328,98	0	0	0	0	0	0
NM_177633	0	328,98	0	0	0	0	0
NM_177909	0	0	98,76	0	0	0	0
NM_008104	0	388,94	388,94	388,94	0	0	0
NM_173770	0	388,94	388,94	388,94	0	0	0
NM_178757	0	0	0	0	2	0	0
NM_001110204	0	0	0	0	98,76	0	0
NM_007669	0	0	0	0	0	0	328,98
NR_004853	0	0	0	0	388,94	388,94	388,94


Any suggestion?

Many many thanks,
Paolo

Last edited by joeyg; 09-06-2013 at 10:35 AM.. Reason: corrected spelling in title
# 2  
Old 09-06-2013
Hello,

with grep:
Code:
grep -v '^[^[:space:]]*\([[:space:]]*0\)*$' file

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 09-06-2013
Many many thanks!
Paolo
# 4  
Old 09-06-2013
Or, a bit simpler:
Code:
 grep -v '^[^[:space:]]*[[:space:]0]*$' file

# 5  
Old 09-07-2013
Using awk
Code:
awk -F\\t '{for (i=2;i<=NF;i++) a+=$i} a{print;a=0}' file

But I do like the regex approach, since no loops are involved
Code:
awk '!/^[^[:space:]]*[[:space:]0]*$/' file

# 6  
Old 09-07-2013
Using patterns may not work reliably, because there are many ways to represent a zero value, for example .0 00 0.0 +0.0 , etc.. So an approach like Jotne's would be preferable, but the method of addition will fail if there are non-zero values that add up to 0.

But something like this should work:
Code:
awk '{for (i=2;i<=NF;i++) if($i){print; next}}'  file


Last edited by Scrutinizer; 09-07-2013 at 04:05 PM.. Reason: No need to select TAB as field separator.
# 7  
Old 09-07-2013
It's right, so just change the regular expression to search for a nonzero number after first "space" separator:

Code:
grep '[[:space:]].*[1-9]' file

Regards
These 2 Users Gave Thanks to disedorgue For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #379
Difficulty: Medium
Solaris is UNIX V7, UNIX 03 and UNIX 98 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines that are subsets of other lines in File

Hello everyone, Although it seems easy, I've been stuck with this problem for a moment now and I can't figure out a way to get it done. My problem is the following: I have a file where each line is a sequence of IP addresses, example : 10.0.0.1 10.0.0.2 10.0.0.5 10.0.0.1 10.0.0.2... (5 Replies)
Discussion started by: MisterJellyBean
5 Replies

2. Shell Programming and Scripting

Remove lines with duplicate pairs where AB is equal to BA

I have a file with four columns like dmn10003t1 PF00001 PF00022 dmn12390t1 dmn10008t1 PF00069 PF00027 dmn9781t1 dmn10008t1 PF00068 PF00027 dmn9781t1 dmn10008t1 PF00069 PF00069 dmn9781t1 dmn12390t1 PF00069 PF00076 dmn10003t1 I want to create a new file by comparing the repeated word pairs... (2 Replies)
Discussion started by: sammy777
2 Replies

3. Shell Programming and Scripting

Remove lines in file

I have a file that contains the following: Party_Id1;Party_id2;Party_id3; 1;2;3; 0 0 4;5;6; 0 7;8;9; How can I adjust the file so it looks like this: Party_Id1;Party_id2;Party_id3; 1;2;3; 4;5;6; 7;8;9; I Think the '0' is something like a carriage return, I don't know. But how... (2 Replies)
Discussion started by: katled
2 Replies

4. UNIX for Dummies Questions & Answers

Want to remove all lines but not latest 50 lines from a file

Hi, I have a huge file which has Lacs of lines. File system got full. I want your guys help to suggest me a solution so that I can remove all lines from that file but not last 50,000 lines. I want solution which can remove lines from existing file so that I can have some space left with. (28 Replies)
Discussion started by: prashant2507198
28 Replies

5. Shell Programming and Scripting

Remove lines from file

Hey Gang- I have a list of servers. I want to exclude servers that begin with and end with certain characters. Is there an easy command to do this? Example wvm1234dev wvm1234pro uvm1122dev uvm1122bku uvm1344dev I want to exclude any lines that start with "wvm" OR "uvm" AND end... (7 Replies)
Discussion started by: idiotboy
7 Replies

6. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

7. Shell Programming and Scripting

Scripting help: Split a file into equal number of lines.

Experts, I have a file datafile.txt that consists of 1732 Line, I want to split the file into equal number of lines with 10 file. (The last file can have 2 line extra to match 1732) Please advise how to do that, Thanks in advance.. (2 Replies)
Discussion started by: rveri
2 Replies

8. Shell Programming and Scripting

remove : lines from file

A small question I have a test.txt file I have contents as: a:google b:yahoo : c:facebook : d:hotmail How do I remove the line with : my output should be a:google b:yahoo c:facebook d:hotmail (5 Replies)
Discussion started by: aronmelon
5 Replies

9. Shell Programming and Scripting

remove lines from file

Hi gurus, i'm trying to remove a number of lines from a large file using the following command: sed '1,5000d' oldfile > newfile Somehow the lines in the old file are not deleted... Am I doing this wrongly? Any suggestions? :confused: Thanks! :) wee (10 Replies)
Discussion started by: lweegp
10 Replies

10. UNIX for Dummies Questions & Answers

vi to remove lines in file

All, I have a text file with several entries like below: personname personname.domain.com I know there is a way to use vi to remove only the personname.domain.com line. Can someone help? I believe that it involves /s/g/ something...I just can't remember the exact syntax. Thanks (2 Replies)
Discussion started by: kjbaumann
2 Replies

Featured Tech Videos