awk filter by occurence of at least two columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk filter by occurence of at least two columns
# 1  
Old 04-16-2012
awk filter by occurence of at least two columns

Hi,
Using AWK script I want to pick those rows that has AT LEAST TWO columns EACH has a count >=3.
i.e. two conditions: at least two columns, each of which has a count at least 3.
There must be a simple way to do this job, but could not figure it out by myself.
Input file (thousand of rows!):
Code:
a    0  0  0   3   1   4
b    0  0  2   0   5   1
c    3  0  0   0   1   0
d    2  0  0   5   4   1
e    7  0  4   4   1   0
f    8  0  0   0   1   3

Output file:
Code:
a    0  0  0   3   1   4
d    2  0  0   5   4   1
e    7  0  4   4   1   0
f    8  0  0   0   1   3

Any suggestion for this problem? Thanks a lot!
# 2  
Old 04-16-2012
try this:
Code:
awk '{x=0;for(i=1;i<=NF;i++) if($i>=3) x+=1; if(x>2)print}' infile

# 3  
Old 04-16-2012
Awesome!
Should the second condition be:
Code:
if (x>=2) print

as my condition is "at least two columns" ?
Thanks a lot!
# 4  
Old 04-16-2012
it should be x>2 because for 1st column ($i>=3) returns true, so "x" increments 1 for 1st column.

Last edited by 47shailesh; 04-16-2012 at 03:21 PM..
This User Gave Thanks to 47shailesh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to filter out consecutive occurence of digits from file?

Hello, I have a text file containing the following for example: This number 123456 is in line number one. Line number two contains -333444 number. The third line has a 111111 in between and also 435. Line number four has :444444 in it. Line five has a in front like a555555. how can i... (5 Replies)
Discussion started by: james2009
5 Replies

2. Shell Programming and Scripting

awk - find first occurence and replace it

Hello, I have a requirement to replace the whole string with first occurence of value of a key-value pair. I have to do this inside awk, as the data I need to work is inside an awk loop. value of my variable(say LogText) looks like: LogText= date=04Mar message=hello1 name=caq... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

3. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

4. UNIX for Dummies Questions & Answers

Solaris - Filter columns in text file and adding new column

Hello, I am very now to this, hope you can help, I am looking into editing a file in Solaris, with dinamic collums (lenght varies) and I need 2 things to be made, the fist is to filter the first column and third column from the file bellow file.txt, and create a new file with the 2 filtered... (8 Replies)
Discussion started by: jpbastos
8 Replies

5. Shell Programming and Scripting

awk filter by columns of file csv

Hi, I would like extract some lines from file csv using awk , below the example: I have the file test.csv with in content below. FLUSSO;COD;DATA_LAV;ESITO ULL;78;17/09/2013;OL ULL;45;05/09/2013;Apertura NP;45;13/09/2013;Riallineamento ULLNP;78;17/09/2013;OL NPG;14;12/09/2013;AperturaTK... (6 Replies)
Discussion started by: giankan
6 Replies

6. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

7. Shell Programming and Scripting

Delete until Nth occurence (sed, awk)

Hello people, Once more I need your help with SED/AWK I need to delete up to the Nth occurence of a char (from the beggining) and until the Mth occurence of a char (from the end) Example: Input: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z Output: i,j Must delete up to the... (2 Replies)
Discussion started by: drbiloukos
2 Replies

8. Shell Programming and Scripting

How to filter a table by two columns

Dear Forum, I would like to know how could I found every result from one column at a table based at two table. Exemplo: Table: Red 4 Red 5 Red 10 Black 33 Black 44 Black 5 Green 2 Green 55 Green 78 I would like to have every color with the lower result... (12 Replies)
Discussion started by: lColli
12 Replies

9. Shell Programming and Scripting

Printing the continuous occurence of pattern using awk ?

Hello all, I have a input file like this. input file --------------- abc ab001 + ab002 zca acb ab006 + ab007 caz cba ab003 + ab004 zca bac ab004 - ab005 zac bca ab002 - ab003 cza cba ab005 + ab006 acz cba ab005 ... (5 Replies)
Discussion started by: admax
5 Replies

10. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies
Login or Register to Ask a Question