awk - How to get rows with values more than 2 in at least 2 columns? -


i trying extract row value >=2 in atleast 2 column. input file

gain,top1,sos1,pho1 atc1,0,0,0 atc2,1,2,1 atc3,6,6,0 atc4,1,1,2 

and awk script

cat input_file | awk 'begin{fs=",";ofs=","};{count>=0;for(i=2; i<4; i++) {if($i!=0) {count++}};if (count>=2){print $0}}' 

which doesn't give me expected output should be

gain,top1,sos1,pho1 atc3,6,6,0 

what problem script. thanks.

awk -f, 'fnr>1{f=0; for(i=2; i<=nf; i++)if($i>=2)f++}f>=2 || fnr==1' file 

or below one, print , go next line after finding 2 values (reasonably faster)

awk -f, 'fnr>1{f=0; for(i=2; i<=nf; i++){ if($i>=2)f++; if(f>=2){ print; next} } }fnr==1' file 

explanation

awk -f, '                                # call awk , set field separator comma          fnr>1{                          # wanna skip header checked so, if no of records related current file greater 1                  f=0;                    # set variable f = 0                  for(i=2; i<=nf; i++)    # start looping second field no of fields in record/line/row                  {                      if($i>=2)f++;        # if field value greater 2 increment variable f                     if(f>=2)             # if got 2 values ?                     {                         print;            # print record/line/row                        next              # got enough go next line                     }                   }                 }fnr==1                   # if first record being read print in fact if fnr==1 boolean true, default operation print $0, current record/line/row         ' file 

input

$ cat file gain,top1,sos1,pho1 atc1,0,0,0 atc2,1,2,1 atc3,6,6,0 atc4,1,1,2 

output-1

$ awk -f, 'fnr>1{f=0; for(i=2; i<=nf; i++)if($i>=2)f++}f>=2 || fnr==1' file gain,top1,sos1,pho1 atc3,6,6,0 

output-2 (reasonably faster)

$ awk -f, 'fnr>1{f=0; for(i=2; i<=nf; i++){ if($i>=2)f++; if(f>=2){ print; next} } }fnr==1' file gain,top1,sos1,pho1 atc3,6,6,0 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -