【问题标题】:Map the corresponding values of a file in shell script在 shell 脚本中映射文件的对应值
【发布时间】:2021-07-30 21:05:14
【问题描述】:

我的 output.txt 文件有两列。我正在尝试添加 HTML 代码来更改将显示数据的单元格的背景颜色。如果该值大于 70,我将添加红色背景颜色并将该值与其对应的主机名一起保存在 .csv 文件中。但是,以下代码无法按要求正常工作。我应该编写什么代码,以便将值及其对应的主机名保存在 .csv 文件中??

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

【问题讨论】:

    标签: shell loops unix awk


    【解决方案1】:

    使用您显示的尝试/示例,您能否尝试使用 GNU awk 进行以下、编写和测试,应该可以在任何工作中使用。请确保您有一个名为path 的shell 变量,因为它作为变量传递给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
    

    【讨论】:

    • 使用您提供的代码后出现以下错误:+ awk -v pathVal=/app/siebel/PSSBLEA073/ent/scripts/diskspace/diskpace_report $'\nBEGIN{\n FS=OFS=","\n outputFile=pathVal"/output.csv"\n}\nFNR>1{\n if($2>70){\n print $1 OFS"
      " OFS $2 > (outputFile)\n }\n if($2>65 && $2" OFS $2 > (outputFile )\n }\n}' output.txt awk:第 1 行附近的语法错误 awk:在第 1 行附近退出
    • @ShubhamChaurasia,您好像在使用 Sun os,请将我的解决方案中的 awk 更改为 nawk,然后运行一次?
    • 这是我给的代码: path=/app/siebel/PSSBLEA073/ent/scripts/diskspace/diskpace_report awk -v pathVal="$path" ' BEGIN{ FS=OFS=", " outputFile=pathVal"/output.csv" } FNR>1{ if($2>70){ print $1 OFS "
      " OFS $2 > (outputFile) } if($2>65 && $2" OFS $2 > (outputFile) } }' output.txt output.txt 是我的输入文件
    • 我不需要这个逗号 b/w 的 div 类以及由于哪个 div 类不适用于值 POIMAPP150,
      ,68 PSSBLEA073,,67 POIDAPP2120,
      ,68 PSSBLSOOIMA200,
      ,68
    • @ShubhamChaurasia,您应该尝试:path=/app/siebel/PSSBLEA073/ent/scripts/diskspace/diskpace_report; nawk -v pathVal="$path" 'BEGIN{FS=OFS=",";outputFile=pathVal"/output.csv"}FNR&gt;1{if($2&gt;70){print $1 OFS "&lt;div class=\"c1\"&gt;" OFS $2 &gt; (outputFile)};if($2&gt;65 &amp;&amp; $2&lt;70){print $1 OFS "&lt;div class=\"w1\"&gt;" OFS $2 &gt; (outputFile)}}' Input_file 使用 nawk,就像您在 Solaris OS 上一样
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签