Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash如果在grep中的一堆文件中找到某个字符串,则执行以下操作_Linux_Bash_Shell - Fatal编程技术网

Linux Bash如果在grep中的一堆文件中找到某个字符串,则执行以下操作

Linux Bash如果在grep中的一堆文件中找到某个字符串,则执行以下操作,linux,bash,shell,Linux,Bash,Shell,我有一堆设备配置文件,希望检查它们的版本是否与我的列表匹配 首先,我需要检查他们的模型 第二步:对于每个型号,只批准特定版本。 例如: 这是配置文件的示例 [user@linux]$ more Device* :::::::::::::: DeviceA :::::::::::::: DeviceA#sh ver Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTW

我有一堆设备配置文件,希望检查它们的版本是否与我的列表匹配

首先,我需要检查他们的模型

第二步:对于每个型号,只批准特定版本。 例如:

这是配置文件的示例

[user@linux]$ more Device*
::::::::::::::
DeviceA
::::::::::::::
DeviceA#sh ver
Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceB
::::::::::::::
DeviceB#sh ver
Cisco IOS Software, 2800 Software (C2800NM-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceC
::::::::::::::
DeviceC#sh ver
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE8, RELEASE SOFTWARE (fc2)
[user@linux]$
这是我从每个配置文件检查模型的脚本

#!/bin/bash
if grep --color=auto 'Cisco IOS Software' * > /dev/null
then
    echo found
else
    echo not found
fi
样本输出

[user@linux]$ ./script.sh
found
[user@linux]$
我能够grep模型,这是第一步。但仍然无法确定如何验证该特定型号是否具有批准的版本

所需输出

如果已批准版本,则结果应为OK。 否则,设备使用的是未经批准的版本

示例

DeviceA - Model 1841 - Version 15.1(4)M10 = OK
DeviceB - Model 2800 - Version 15.1(4)M10 = OK
DeviceC - Model C2960, Version 12.2(55)SE8 = NOT OK

我希望这个问题足够清楚,如果不清楚,请告诉我。

下面的脚本中有注释:

# our devices input file
cat <<EOF >devices
::::::::::::::
DeviceA
::::::::::::::
DeviceA#sh ver
Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceB
::::::::::::::
DeviceB#sh ver
Cisco IOS Software, 2800 Software (C2800NM-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceC
::::::::::::::
DeviceC#sh ver
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE8, RELEASE SOFTWARE (fc2)
EOF

# approved list
cat <<EOF >appr
Model 1841, Version 15.1(4)M7, 15.1(4)M10, or 15.2(1)T4 are approved.
Model 2800, Version 15.1(2)GC2, 15.1(4)M10, 15.1(4)M12a are approved.
Model C2960, Version 15.0(2)SE9 and 15.0(2)SE11 are approved.
EOF

# parse that appr list into "<mode> <version>\n" list
# get rid of all that "are approved." and "," and "Version" and "and" and "or"
# it's just lucky it works
<appr sed -e 's/Model \([^,]*\), Version \(.*\) are.*/\1 \2/' \
    -e 's/and//g' -e 's/or//' -e 's/, / /g' -e 's/[[:space:]]\+/ /g' |
# puff, I'm out of ideas, for each model and versions reformat the list
while IFS=", " read -r model versions; do
    while IFS=" " read -d' ' -r version; do
        printf "%s %s\n" "$model" "$version"
    done <<<"$versions"
done > ./appr2
# OK, after we have that, we can move on

#extract "<device>\n<model> <version>\n" from the list of devices
<file sed -n '/sh ver/s/\([^#]*\).*/\1/p; /Cisco/s/[^,]*, \([^ ]*\).*Version \([^,]*\).*/\1 \2/p' |
while 
    # simple read liine and line and model
    IFS= read -r device &&
    IFS=' ' read -r model version
do
    # because of it is formatted, that's pretty and simple
    if grep -q -f ./appr2 <<<"$model $version"; then
        approved_str=""
    else
        approved_str="NOT "
    fi

    # output
    printf "%s - Model %s - Version %s = %sOK\n" "$device" "$model" "$version" "$approved_str"
done        
C2960型
后没有逗号,我想这是打字错误

脚本中最糟糕的部分是所有正则表达式解析。它可以很容易地打破,我不知道什么样的输入它将工作。如果正确设置输入文件的格式,整个脚本将变成:

grep -q -f ./approved_list ./devices_list

使用格式为换行分隔的
/approved\u list
和格式为
\n
/devices\u list
的ex.
/approved\u list
可以创建如下自定义approved.cfg

$ cat approved.cfg
1841.*Version 15.1\(4\)M7
2800.*Version 15.1\(2\)GC2
C2960.*Version 15.0\(2\)SE9
1841.*Version 15.1\(4\)M10
2800.*Version 15.1\(4\)M10
C2960.*Version 15.0\(2\)SE11
1841.*Version 15.2\(1\)T4
2800.*Version 15.1\(4\\)M12a
现在,您可以使用此选项从设备进行灰显:

echo Hits
grep -Ef approved.cfg Device*
echo Noks
grep -vEf approved.cfg Device* | grep "Cisco IOS Software"
您可以调整输出以符合要求。
更改您的
approved.cfg

1841 - Version 15.1(4)M7
2800 - Version 15.1(2)GC2
C2960 - Version 15.0(2)SE9
1841 - Version 15.1(4)M10
2800 - Version 15.1(4)M10
C2960 - Version 15.0(2)SE11
1841 - Version 15.2(1)T4
2800 - Version 15.1(4)M12a
首先查找正在工作的
sed
命令:

grep -H "Cisco IOS" Device* | 
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/'
现在你可以把它和

grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Fvf approved.cfg |
   sed 's/$/ = NOT OK/'

grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Ff approved.cfg |
   sed 's/$/ = OK/'

批准的版本列表在哪里?多长时间?它看起来像什么?@MarkSetchell,它在第二步。
second step
这是一个格式糟糕的版本列表。设备列表中甚至没有
型号1841
。你打算用正则表达式来解析它吗?
已批准。
只是毫无意义-此列表中的所有设备均已批准。。。如果这与特定的行匹配,最好首先存储正则表达式,或者存储简单的类似CSV的字符串,如
1841 | Version 15.1(4)M7
,这样您就可以解析它了。设备字符串是否保持不变?解析它有意义吗?根据您介绍的内容,只需使用regex+sed从设备列表中提取型号和版本,然后
使用解析后的approved model+version列表加入
。@KamilCuk,您是对的。我要用正则表达式来做这个。让我修改代码并更新帖子。
grep -H "Cisco IOS" Device* | 
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/'
grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Fvf approved.cfg |
   sed 's/$/ = NOT OK/'

grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Ff approved.cfg |
   sed 's/$/ = OK/'