Awk

To find the number of lines in a file use wordcount! wc

awk '/q/{print FILENAME,$1,$2}' file.ascii

prints the filename of the program that awk is awking.

VPFIT fort.26 program:

summary.awk

# This awk program looks up results from VPFIT and displays them nicely
/%%/ {print "File Used: ",$2}
/%%/ {print "Wavelength Range: ",$4,":",$5}
/FeII/ {print $1,"Log(N)",$6,"Error",$7}
/FeII/ {print $1,"b-val",$4,"Error",$5}
/FeII/ {print $1,"Redshift",$2,"Error",$3,"Error",$3*2.99e5,"km/s",$3*2.99e8,"m/s"}
/Stats/ {print $2}
/Stats/ {print "Iterations: ",$3}
/Stats/ {print "Chi-squared: ",$4}
/Stats/ {print "Channels: ",$5}
/Stats/ {print "Degrees of freedom: ",$6}

and you run it with:

$ awk -f summary.awk fort.26

This script takes the output from Marcy into a format that DUDE can read.

awk '{printf("%.15f  %10.9f %f\n",$1,$2/5000.0000,0.000001)}' iodinefull.ascii > chad2.ascii

This script takes the output from DUDE with the continuum fit by eye into a normalized output.

awk '{printf("%.15f  %10.9f\n",$2,$3/$6)}' endoftheworld.ascii > alliodinenorm.ascii

Print the line that the item is found: use "FNR" or "NR"

 awk '{print "set arrow",FNR,"from", $1",0 to ",$1",3 nohead linetype 3"}' trash1

To put conditionals on what the next action to do, the format is like:

awk '{if ($1>0) print $2}' noI2_62.ascii

and of course things like this search for stuff just like in the program at the top:

awk '/Fe/{print $2}' noI2_62.ascii

To take the output from DUDE and put it into the format that my c-program takes, I use this code:

awk '{printf("%.4f %.10f %.10f\n",$2,(($3-$5)/$6),($4/$6))}' B62.ascii > I2_62.ascii

It reads in the Dude data, and prints out the wavelength $2, the difference between the flux $3 and the fit $5 divided by the continuum $6, and the error $4 divided by the continuum $6.

You can use awk within a bash code to do some very useful things…

This following thing reads file.name and stores the text into a matrix called Q. Then, all sorts of fun things can be done…

awk '{
    line++;
    for(i=1;i<=NF;i++){Q[line,i]=$i;}
}
END{
    for (i=1;i<=NR;i++){
        if ( Q[i,3]>0 ){
            printf("%10.6f %10.6f\n",Q[i,1],Q[i-1,2]);
        }
    }

}' file.name

And you can get all sorts of fancy with the number of files your read in separately… assigning things to different values that will both be used in the END part of the code w/in the bash script…

    awk '{
        if(FILENAME=="'sortedcutout.ascii'"){
            line++;
            for(i=1;i<=NF;i++){absorbmatrix[line,i]=$i;}
            columns=NF;
        }
        if(FILENAME=="'Output/calculation_$order.ascii'"){
            numkecklines++;
            for(i=1;i<=NF;i++){calcmatrix[numkecklines,i]=$i;}
            numofshifts=NF;
        }
    }
        END{
        sizeofbins='$sizeofbin';
    for(j=1;j<=line;j++){
        if(calcmatrix[2,1] < absorbmatrix[j,2] - sizeofbins && calcmatri
x[numkecklines,1] > absorbmatrix[j,3] + sizeofbins){
            if(absorbmatrix[j,2]<calcmatrix[numkecklines,1]&&absorbm
atrix[j,2]>calcmatrix[2,1]){
                for(i=1;i<=numkecklines;i++){
                    leftlow=0;
                    while(calcmatrix[i,1]<absorbmatrix[j,2] 
- sizeofbins){
                        leftlow++;
                        i++;
                    }
                    i+=i+leftlow
                    leftbegin[j]=leftlow+1;
                }
                for(i=1;i<=numkecklines;i++){
                    lefthigh=0;
                    while(calcmatrix[i,1]<absorbmatrix[j,2])
{
                        lefthigh++;
                        i++;
                    }
                    i+=i+lefthigh
                    leftendpt[j]=lefthigh+1;
                }
                for(i=1;i<=numkecklines;i++){        
                    rightlow=0;
                    while(calcmatrix[i,1]<absorbmatrix[j,3])
{
                        rightlow++;
                        i++;
                    }
                    i+=i+rightlow
                    rightbegin[j]=rightlow+1;
                }
                for(i=1;i<=numkecklines;i++){
                    righthigh=0;
                    while(calcmatrix[i,1]<absorbmatrix[j,3] 
+ sizeofbins){
                        righthigh++;
                        i++;
                    }
                    i+=i+righthigh
                    rightendpt[j]=righthigh+1;
                }
            }
        }
    }

    for(j=1;j<=line;j++){
        if(leftbegin[j]>0){
            for(k=2;k<=numofshifts;k++){
                for(i=leftbegin[j];i<=leftendpt[j];i++){
                    sumleft[j,k]+=calcmatrix[i,k];
                }
            }
        }
        if(rightbegin[j]>0){
            for(k=2;k<=numofshifts;k++){
                for(i=rightbegin[j];i<=rightendpt[j];i++){
                    sumright[j,k]+=calcmatrix[i,k];
                }
            }
        }
    }

    for(j=1;j<=line;j++){
        if(leftbegin[j]>0){
            print "#", "left", absorbmatrix[j,1], absorbmatrix[j,4]
            for(k=2;k<=numofshifts;k++){
                print calcmatrix[1,k], sumleft[j,k]
            }
        }
        print ""
        print ""
        if(rightbegin[j]>0){
            print "#", "right", absorbmatrix[j,1], absorbmatrix[j,4]
            for(k=2;k<=numofshifts;k++){
                print calcmatrix[1,k], sumright[j,k]
            }
        }
        print ""
        print ""
    }

    }' sortedcutout.ascii Output/calculation_$order.ascii > Calibrate/abs_$o
rder.$sizeofbin.ascii
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.