我对 BASH 脚本相对较新。我希望有人能帮忙。我有两个文件。文件 1 是包含某些服务器属性的.csv文件。
cmdb_ci_linux_server.csv
"CLS000","csl000","Linux SuSe","9","HP"
"CLS001","cls001","Linux SuSe","9","VMware, Inc."
"CLS002","cls002","Linux Red Hat","5.11","VMware, Inc."
...
"VSRQ1CS1","vrsq1cs1","Linux SuSe","11","VMware, Inc."
这些是已签出的服务器。我需要将其与所有服务器的列表进行比较,并找出哪些服务器尚未检查。所有服务器的列表采用以下格式:主机.txt
cls000
cls001
cls002
cls003
cls004
cls005
...
cls499
我尝试了几种不同的脚本,但没有一个对我有用。我尝试在单独的脚本中执行所有不同的步骤,希望我能保持相对简单。这对我来说很有意义,但它不会返回任何东西。任何帮助都非常感谢。
#!/bin/bash
while IFS="," read name host_name os os_version manufacturer
do
cat cmdb_ci_linux_server.csv
cat hosts.txt
grep -vf cmdb_ci_linux_server.csv hosts.txt
done
我知道我使用linux的方法,但不是很好。我对窗户更熟悉。我有点出乎意料地投入了这份工作:/
提前感谢!!
试试这个:
(cat cmdb_ci_linux_server.csv |
awk 'BEGIN{FS=","}{print substr($2,2,length($2)-2)}'|
sort | uniq;
cat hosts.txt | sort | uniq) | sort | uniq -c
示例结果:
1 CLS000
2 CLS001
2 CLS002
1 CLS003
1 CLS004
1 CLS005
1 个 CSL000
左数字:表示找到的出现次数
这应该有效:
while read line
do
IFS=","
arr=($line)
text=${arr[1]//"/""}
[[ $(grep $text hosts.txt -c) -le 0 ]] && echo "$text: Not Matched"
done <cmdb_ci_linux_server.csv