Can you please help me? I need help coding the AWK, I can't get through this. Can also be a BASH solution. Code:
1
4
7
5
5
1
4
8
3
6
My desired output is Code:
1 5 .2
4 5 .8
7 12 .58
5 12 .41
5 6 .83
1 6 .16
4 12 .33
8 12 .66
3 9 .5
6 9 .5
As you can see I need to add the even and odd lines together to generate the second column. This value is kept for the 2 lines because I need to do some searching and add results up to get a percent.
After that I need to divide the value in the first column with the value of the same spot in the second column. to get the per cent.
I just can't get it done efficiently. What I'm doing right now is just getting the odd lines in a separate file from the even ones and then attaching the columns and proceeding to create an array for both files and then do the algebra. Must be a simple way with AWK that does not involve temporary files or anything like that.
Upon using dudiboy's advised piece of code on the following text file, these are the incorrect results I'm getting:
This is the list.txt file contents:
1
2
6
6
8
1
2
3
5
root@debian:/home/l0l/Documents/awk# awk 'a[NR]=$1; END {for (ln=2; ln<=NR; ln+=2){ print(a[ln], a[ln]+a[ln-1], a[ln]/a[ln-1]); print(a[ln], a[ln]+a[ln-1], a[ln-1]/a[ln]);}}' list.txt
1
2
6
6
8
1
2
3
5
2 3 2
2 3 0.5
6 12 1
6 12 1
1 9 0.125
1 9 8
3 5 1.5
3 5 0.666667
The percent's are way off and the way that it prints it out is way off as well, will keep trying however I'd really appreciate any help with this. Please guide me step by step about the advised command?
UPDATE: $ awk -v fmt="%-2i %-2i %.2f\n" 'NR%2{x=$1;next} {printf fmt, x,x+$1,x/(x+$1); printf fmt,$1,x+$1,$1/(x+$1)}' file
Is the PERFECT solution !!!! THANKS A ZILLION!! so happy! However when I try to get the output of the command into a file I get a zero divide error
root@debian:/home/l0l/Documents/bash# awk -v fmt="%-2i %-2i %.2f\n" 'NR%2{x=$1;next} {printf fmt, x,x+$1,x/(x+$1); printf fmt,$1,x+$1,$1/(x+$1)}' results >> RESULTS2.txt
awk: cmd. line:1: (FILENAME=results FNR=648) fatal: division by zero attempted
I'm using it on a 2000 part list of numbers and there is sometimes 2 zeros in a row so. so it produces the second column with also a 0 and then since we are dividing by zero we get this zero divide issue. How do we prevent this? I have to keep the 0's in the file, its super important. Maybe a different way to calculate percentage? How to get a nice output without that zero divide? For example if the list file is now:
0
0
1
5
5
6
0
0
0
1
0
0
We get zero divides all over. Any insight of how to prevent this?
Try:
$ awk -v fmt="%-2i %-2i %.2f\n" 'NR%2{x=$1;next} {printf fmt, x,x+$1,x/(x+$1); printf fmt,$1,x+$1,$1/(x+$1)}' file
1 5 0.20
4 5 0.80
7 12 0.58
5 12 0.42
5 6 0.83
1 6 0.17
4 12 0.33
8 12 0.67
3 9 0.33
6 9 0.67
$ awk -v fmt="%-2i %-2i %.2f\n" 'NR%2{x=$1;next} {d=x+$1; printf fmt, x,d,(d?x/(x+$1):0); printf fmt,$1,d,(d?$1/(x+$1):0)}' file2
0 0 0.00
0 0 0.00
1 6 0.17
5 6 0.83
5 11 0.45
6 11 0.55
0 0 0.00
0 0 0.00
0 1 0.00
1 1 1.00
0 0 0.00
0 0 0.00