while 循环每次迭代的回显变量

Echo variable on each iteration of while loop

我正在尝试为警报文件(名为 critical)的每一行回显 information/variables,使用 while read line 获取警报文件的每一行(可能有很多行在 critical文件):

while read line
do
{
s=`grep -Po '(?<=(sender:)).*' critical| cut -d ',' -f1` 
t=`grep -Po '(?<=(time:)).*' critical| cut -d '.' -f1` 
d=`grep -Po '(?<=(.1.3.6.1.4.1.2285.1.1.8:)).*' critical| cut -d ',' -f1` 
echo "Sender: $s" >>critical_body
echo "Time: $t" >>critical_body
echo "Description: $d" >>critical_body
}
done < critical

我需要的输出结构如下:

Sender:  xyz1
Time:  2021/08/11 22:14:19
Description: Lost communication with device

Sender: xyz2
Time: 2021/08/13 15:58:02
Description: Memory usage is above normal: 10% free

但我得到以下信息,似乎变量值是在一次迭代中打印的,用 /n 分隔,而不是我想要的一个一个打印。

Sender:  xyz1
 xyz2
Time:  2021/08/11 22:14:19
 2021/08/13 15:58:02
Description: Lost communication with device
Memory usage is above normal: 10% free
Sender:  xyz1
 xyz2
Time:  2021/08/11 22:14:19
 2021/08/13 15:58:02
Description: Lost communication with device
Memory usage is above normal: 10% free

有人可以帮助我吗?我正在尝试找出问题所在,我在 bash 脚本编写方面相对较新。

cat critical
2021-08-11 22:14:19,550 INFO  [com.optimization.ems.snmp.FnTrapReceiver] raw trap record inserted: com.optimization.ems.ems.model.snmp.RawTrapid: null, oid: .1.3.6.1.4.1.2285.1.2.14, time: 2021/08/11 22:14:19.547, sender: xyz1, version: 2, varbind: .1.3.6.1.2.1.1.3.0:5d 11h 0m 35s 350ms,.1.3.6.1.6.3.1.1.4.1.0:.1.3.6.1.4.1.2285.1.2.14,.1.3.6.1.4.1.2285.1.1.6:xyz1,.1.3.6.1.4.1.2285.1.1.8:Lost communication with device,.1.3.6.1.4.1.2285.1.1.3:5,
2021-08-13 15:58:02,083 INFO  [com.optimization.ems.snmp.FnTrapReceiver] raw trap record inserted: com.optimization.ems.ems.model.snmp.RawTrapid: null, oid: .1.3.6.1.4.1.2285.1.2.3, time: 2021/08/13 15:58:02.79, sender: xyz2, version: 2, varbind: .1.3.6.1.2.1.1.3.0:98d 21h 39m 6s 60ms,.1.3.6.1.6.3.1.1.4.1.0:.1.3.6.1.4.1.2285.1.2.3,.1.3.6.1.4.1.2285.1.1.2:15:58:02 13/08/2021,.1.3.6.1.4.1.2285.1.1.3:5,.1.3.6.1.4.1.2285.1.1.8:Memory usage is above normal: 10% free,

对于你使用 grep 的每一行,你都可以通过管道连接到 echo 命令来一次性输出所有内容。之后你可以grep时间,然后是描述。在描述之后,您可以单独使用 echo 来换行,然后再开始新的循环迭代。

为了调试和故障排除更容易测试发件人发件人的grep,并查看命令行上的输出是否正确,然后您可以将其添加到脚本中,当发件人的输出正确时,其他后面两个应该更容易修改。

s=`grep -Po '(?<=(sender:)).*' critical| cut -d ',' -f1` | echo "Sender: $s" >>critical_body

你可以试试这个脚本。这有点麻烦,可能有更有效的方法来实现这一点。

 for c in critical;           
    do      
    out="critical.outfile"; 
    outNew="crit.out "    ;     
    sed -E 's/.*(sender[^,]*).*//1' "$c" > "$out";           
    sed -E 's/.*(time[^,]*).*//' "$c" >> "$out";          
    sed -E 's/.*[0-9]:([^0-9].*[a-z]),.*/Description: /' "$c" >> "$out";
    awk '!r[]++' "$out" > $outNew;      
    echo >> $outNew;     
    awk 'r[]++' "$out" >> $outNew;
    cat $outNew  
done

输出

sender: xyz1
time: 2021/08/11 22:14:19.547
Description: Lost communication with device

sender: xyz2
time: 2021/08/13 15:58:02.79
Description: Memory usage is above normal: 10% free

像这样:

while read line; do
    time=${line/*time:/}
    time=${time/.*, sender:*/}
    sender=${line/*sender: /}
    sender=${sender/, version:*/}
    description=${line/*.1.1.8:/}
    description=${description/,*/}
    printf 'Sender: %s\nTime: %s\nDescription: %s\n\n' "$sender" "$time" "$description"
done < critical

输出将是:

Sender: xyz1
Time:  2021/08/11 22:14:19
Description: Lost communication with device

Sender: xyz2
Time:  2021/08/13 15:58:02
Description: Memory u sage is above normal: 10% free