shell - 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
解决方案
使用您显示的尝试/示例,您能否尝试使用 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
推荐阅读
- excel - 范围参考 Excel VBA
- php - Laravel Nova 数据重复
- hyperledger-fabric - 使用未反映在 Composer Playground 中的 API 对 REST 服务器所做的更改?
- css - 使元素占用可用的水平空间而不导致其父元素变宽
- uicollectionview - 重新加载 UICollectionView 时 CABasicAnimation 停止
- python - 从网页下载 JSON 数据?
- java - 将 id 从一个 jsp 发送到另一个的问题(更新链接)
- python - 我在发送邮件时遇到问题:TypeError: __init__() got an unexpected keyword argument 'context'
- dart - 如何将多个数据流合并到一个 BLOC 中?
- c# - 如何删除 ASCII 文件中的最后一个字符