首页 > 解决方案 > Map the corresponding values of a file in shell script

问题描述

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

标签: shellloopsunixawk

解决方案


使用您显示的尝试/示例,您能否尝试使用 GNU 编写和测试awk,应该可以在任何工作中使用。请确保您有一个名为的 shell 变量path,因为它作为变量传递给awk程序。

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

推荐阅读