I have a file1.txt with below content
value1=value2=value3=server2=150=200=Running=0
value1=value2=value3=server5=0=100=Running=0
value1=value2=value3=server3=40=100=Running=0
value1=value2=value3=server8=175=100=Running=0
value1=value2=value3=server1=0=100=Running=0
value1=value2=value3=server7=0=100=Running=0
I am doing arithmetic operation between column 5 and column 6 considering equal to(=) as field separator. Code is as below.
awk -F "=" '$9=(($5*100/$6))' file1.txt
But when I am using above code and appending the result in last column its giving result only where the value is not 0 but I need those rows also where the value is 0. Also I would like to sort the rows in descending order based on the last column value.
Below is the result I am getting
vlaue1 value2 value3 server2 150 200 Running 0 75
vlaue1 value2 value3 server3 40 100 Running 0 40
vlaue1 value2 value3 server8 175 200 Running 0 87.5
Expected Result is as follows
vlaue1 value2 value3 server8 175 200 Running 0 87.5
vlaue1 value2 value3 server2 150 200 Running 0 75
vlaue1 value2 value3 server3 40 100 Running 0 40
value1 value2 value3 server5 0 100 Running 0 0
value1 value2 value3 server1 0 100 Running 0 0
value1 value2 value3 server7 0 100 Running 0 0
Please assist me how we can achieve this.
CodePudding user response:
You should explicitly print each line. Also if you want to later sort it, you can use this solution: "How to numerically sort by last column". For example:
awk -F "=" '{$9=$5*100/$6; print $9" "$0}' file1.txt | sort -nr | cut -f2- -d' '
CodePudding user response:
Run it as below. Notice 1 at the end.
"awk -F "=" '{$9=(($5*100/$6))}1' file1.txt"
For explanation see https://stackoverflow.com/a/44011942/3423750
Note: I'm using a mobile browser currently fir writing this answer do the formatting is limited. I will update it later when I access desktop.
