Bereket
Bereket

Reputation: 55

How to output selected elements of a dataset using awk

I have a dataset of numbers arranged in rows and columns. The dataset has "NULL" values, which I want to get rid of. I have used awk to do this and my script is shown below. I don't get an error when I ran it, but there is no output either. I have to do Ctrl+d to abort the process. I can't think of an obvious reason why this won't work; what is wrong here?

{       for (i=1;i<=NF;i+1) 
                if( (NR>2) && ($i !="NULL"))
                        print $i
}

My input looks something like this

  NULL   0.435   0.574
 0.533    NULL    NULL
 0.481   0.460    NULL
 0.378   0.568   0.425
  NULL    NULL    NULL

And my desired output is something like

 0.435   0.574
 0.533   
 0.481   0.460   
 0.378   0.568   0.425

Upvotes: 1

Views: 42

Answers (2)

SiegeX
SiegeX

Reputation: 140307

I think sed would be the better tool here

sed 's/\s*NULL\s*//g' ./infile

Proof of Concept

$ sed 's/\s*NULL\s*//g' ./infile
0.435   0.574
0.533
0.481   0.460
0.378   0.568   0.425

Upvotes: 0

jas
jas

Reputation: 10865

The main problem is the increment part of your for statement. i+1 doesn't change the value of i. Use ++i. Also, you'll want to use printf to control when and when not to print newlines. (Note also that here I've used NR>0 since there weren't any headers in your sample.)

a.awk:

{
    for (i=1;i<=NF;++i) {
      if((NR>0) && ($i !="NULL"))
        printf "%.3f ", $i
    }
    print ""
}

Test:

$ awk -f a.awk file
0.435 0.574 
0.533 
0.481 0.460 
0.378 0.568 0.425 

Slightly fancier version for controlling better the field separator:

{
    sep=""
    for (i=1;i<=NF;++i) {
      if((NR>0) && ($i !="NULL")) {
        printf "%s%.3f", sep, $i
        sep=OFS
      }
    }
    print ""
}

Comma separator:

$ awk -f a.awk OFS=, file
0.435,0.574
0.533
0.481,0.460
0.378,0.568,0.425

Tab separator:

$ awk -f a.awk OFS='\t' file
0.435   0.574
0.533
0.481   0.460
0.378   0.568   0.425

Upvotes: 2

Related Questions