Add new column which is subtract of 2 columns.

Hi below is a file

Date	         Category	        Time	        Attempts	Success
2/17/2014	 PayFlow ATB	0.999988	4039104	4039057
2/18/2014	 PayFlow ATB	0.999912	4620964	4620558
2/19/2014	 PayFlow ATB	0.999991	4380836	4380796
2/20/2014	 PayFlow ATB	0.999988	5031047	5030985
2/21/2014	 PayFlow ATB	0.999842	4408205	4407510
2/22/2014	 PayFlow ATB	0.999991	3228407	3228377
2/23/2014	 PayFlow ATB	0.999938	3392795	3392585

My desired output is to add a new column called failure
Failure = Attempts-Success

Need a unix command to have a new file with this 6th column also...
Plz do help

awk '{ print $0, (NR > 1) ? $5+$6 : "Failure" }' inputfile

Small adaptions to hergp's proposal, as TAB seems to be the separator, and the difference is requested:

awk '{ print $0 "\t" (NR > 1) ? $5-$6 : "Failure" }' inputfile
1 Like

Hello,

Following may also help for same.

awk 'NR==1 {print "Failure"} NR > 1 {{a=$5-$6} {print a}}'  file_name

EDIT: One more solution for same.

awk 'NR==1 {print "Failure"} NR > 1 {{a=$(NF-1)-$(NF)} {print a}}'  file_name

Output will be as follows.

Failure
47
406
40
62
695
30
210

NOTE: For taking the output in a new file please use > redirection operator for same.

Thanks,
R. Singh

awk '
        NR == 1 {
                $0 = $0 OFS "Failure"
        }
        NR > 1 {
                $( NF + 1 ) = $5 - $6
        }
        1
' OFS='\t' file

Thanks a lot for all members who helped. Its working now. Thanks again.