Deleting row if all column values are a particular string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting row if all column values are a particular string
# 1  
Old 10-19-2012
Deleting row if all column values are a particular string

Hello,

I have a very large file for which I would like to remove all rows for which the value of columns 2-5 is zero.

For instance I would like this file:
Code:
contig1, 0, 0, 0, 0 
contig2, 1, 3, 5, 0 
contig3, 0, 0, 0, 0 
contig4, 0, 5, 6, 7

To become this file:
Code:
contig2, 1, 3, 5,0 
contig4, 0, 5, 6, 7

I'm aware that you can use 'Awk' to remove rows for which a particular column has a specific value, but I can't seem to figure out how to remove a row if multiple columns have to have a particular value (i.e zero).

Thanks!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-19-2012 at 01:18 PM.. Reason: code tags, please!
# 2  
Old 10-19-2012
Code:
 
awk -F, '!($2==0 && $3==0 && $4==0 && $5==0)' infile

# 3  
Old 10-19-2012
Code:
awk '{n=0;for(i=2;i<=5;i++) if ($i+0!=0) n++}n' FS=',' myFile

# 4  
Old 10-19-2012
When I run this script (from rdrtx1), it seems to have taken out all of the rows that have any zeros and not just rows for which all four columns have zeros. Also, is there a way to print the data following row removal into a new file?
# 5  
Old 10-19-2012
Quote:
Originally Posted by mouchkam
When I run this script (from rdrtx1), it seems to have taken out all of the rows that have any zeros and not just rows for which all four columns have zeros. Also, is there a way to print the data following row removal into a new file?
just redirect the output to a new file....
# 6  
Old 10-19-2012
Quote:
Originally Posted by vgersh99
Code:
awk '{n=0;for(i=2;i<=5;i++) if ($i+0!=0) n++}n' FS=',' myFile

When I run this script nothing seems to happen? Is the output being directed somewhere? Sorry for my ignorance.
# 7  
Old 10-19-2012
Quote:
Originally Posted by mouchkam
When I run this script nothing seems to happen? Is the output being directed somewhere? Sorry for my ignorance.
you should see the output on your screen like so:
Code:
$ awk '{n=0;for(i=2;i<=5;i++) if ($i+0!=0) n++}n' FS=, myFile
contig2, 1, 3, 5, 0
contig4, 0, 5, 6, 7

(assuming myFile is the name of a file you're trying to parse)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

How to add one to each row values and keep it after the value in the column?

Dear Folks Hello I have a column of numbers, say: 26 79 68 I want to add one to each row value and get this desire column: 26 27 79 80 68 69 (6 Replies)
Discussion started by: sajmar
6 Replies

2. Shell Programming and Scripting

Parsing Column Values in Row

Hi , I have been trying to write a awk script which will have the following Output 1. Append the last Characters of the lines matching pattern xxxxxxxxx & then a space & then Append the last Characters of the lines matching pattern yyyyyyyyy 2. the process will continue till end of file &... (10 Replies)
Discussion started by: newageBATMAN
10 Replies

3. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Deleting a row based on fetched value of column

Hi, I have a file which consists of two columns but the first one can be varying in length like 123456789 0abcd 123456789 0abcd 4015 0 0abcd 5000 0abcd I want to go through the file reading each line, count the number of characters in the first column and delete... (2 Replies)
Discussion started by: swasid
2 Replies

6. Shell Programming and Scripting

Arrange values of a column in row

HI, I need to arrange values of a colum in row. e.g. input file : Alpha<>123 AAAA<>6754 Beta<>456 BBBB<>63784 CCC<>783 Gama<>789 Alpha<>555 AAAA<>6754 BBBB<>63784 Beta<>666 CCC<>783 Gama<>888 (9 Replies)
Discussion started by: The_Archer
9 Replies

7. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (6 Replies)
Discussion started by: Pep Puigvert
6 Replies

8. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (1 Reply)
Discussion started by: Pep Puigvert
1 Replies

9. Shell Programming and Scripting

Converting values in a ROW to COLUMN

Hi All, I needd to convert values in a row to a column. eg: Input is as: value1,value2,value3,value4,.........,value N Required Output: Value1 Value2 Value3 . . . Value N Please help.... (3 Replies)
Discussion started by: sambaman
3 Replies
Login or Register to Ask a Question