Map the corresponding values of a file in shell script

Viewed 86

My output.txt file is having two columns. I'm trying to add HTML code to change the background color of the cell where the data will be displayed. If the value is greater than 70, I'm adding a red background color and saving that value along with it's corresponding hostname in the .csv file. But, the below code is not working properly as required. What code should I write so that the value along with it's corresponding hostname is saved in the .csv file??

Output.txt file:

Hostname, Value
POIDAPP1122,60
POIMAPP150,68
PSSBLEA072,66
PSSBLEA073,65
PSSBLEMA041,66
PSSBLEMA104,60
POIDAPP2120,68
POIMAPP149,60
POAMAPP1121,61
PSSBLEA111,60
PSSBLEAIW133,67
PSSBLEAIW143,61
PSSBLSSOOAMA076,64
PSSBLSSOOIMA200,68
plsbproxyw123,61


for i in `cat $path/output.txt| sed "1 d"`; do
Shostname=$(echo $i |awk '{print $1}');      
Svalue=$(echo $i |awk '{print $2}');      
if [ $Svalue -gt 70 ]
then
echo "Shostname,<div class="c1">$Svalue" >>$path/output_new.csv
elif [ $Svalue -gt 65 ]
then
echo "$Shostname,<div class="w1">$Svalue" >>$path/output_new.csv
then
echo "$Shostname,$Svalue" >>$path/output_new.csv
fi
done

  Getting this output:  


HOSTNAME, SPACE(%)
66  66
66  66
67  67
68  68
68  68
68  68
65
65
65
65
65
65
65
PSSBLEA073  66
PSSBLEA072  66
PSSBLEMA041 66
PSSBLEAIW133    66
PSSBLSSOOIMA200 66
POIDAPP2120 66
POIMAPP150  66
PSSBLEA073  66
PSSBLEA072  66
PSSBLEMA041 66
PSSBLEAIW133    66
PSSBLSSOOIMA200 66
POIDAPP2120 66
POIMAPP150  66
PSSBLEA073  67
PSSBLEA072  67
PSSBLEMA041 67
PSSBLEAIW133    67
PSSBLSSOOIMA200 67
POIDAPP2120 67
POIMAPP150  67
PSSBLEA073  68
PSSBLEA072  68
PSSBLEMA041 68
PSSBLEAIW133    68
PSSBLSSOOIMA200 68
POIDAPP2120 68
POIMAPP150  68
PSSBLEA073  68
PSSBLEA072  68
PSSBLEMA041 68
PSSBLEAIW133    68
PSSBLSSOOIMA200 68
POIDAPP2120 68
POIMAPP150  68
PSSBLEA073  68
PSSBLEA072  68
PSSBLEMA041 68
PSSBLEAIW133    68
PSSBLSSOOIMA200 68
POIDAPP2120 68
POIMAPP150  68
PSSBLEA073  65
PSSBLEA072  66
PSSBLEMA041 66
PSSBLEAIW133    67
PSSBLSSOOIMA200 68
POIDAPP2120 68
POIMAPP150  68

This is my desired output:
  HOSTNAME SPACE(%)
 PSSBLEA073    65
 PSSBLEA072    66
 PSSBLEMA041   66
 PSSBLEAIW133  67
PSSBLSSOOIMA200 68
 POIDAPP2120   68
 POIMAPP150    68
1 Answers

With your shown attempts/samples, could you please try following, written and tested with GNU awk, should work in any work. Please make sure you have a shell variable named path because its being passed to awk program as a variable.

awk -v pathVal="$path" '
BEGIN{
  FS=OFS=","
  outputFile=pathVal"/output.csv"
}
FNR>1{
  if($2>70){
    print $1 OFS "<div class=\"c1\">" OFS $2 > (outputFile)
  }
  if($2>65 && $2<70){
    print $1 OFS "<div class=\"w1\">" OFS $2 > (outputFile)
  }
}' Input_file
Related