我想在bash脚本的输出中添加错误的含义

问题描述 投票:0回答:1

我有一个源文件,其中填充了这些行。

e9999=$"First named error has occurred"
e1000=$"Second named error has occurred"
e0000=$"Another named error has occurred"

我有一个脚本,最后以这样的行输出(该脚本是多次抓入一些sed,然后再进行进一步抓握)。

Dec 03 22:15:49      uniqid c5bb3ed1d2110203e2850998f1008e92   IP 0.0.0.0   [email protected] StatusCode: 1000
Dec 04 07:53:12      uniqid 476ceb443a552012a1dd1e754d279018   IP 255.255.255.255   [email protected] StatusCode: 9999
Dec 04 10:51:08      uniqid 4c27a6e016e8b5c0a659274d20c86e0c   IP 192.168.0.1   [email protected] StatusCode: 0000

我希望将每行状态代码的含义添加到输出的末尾。

示例:

Dec 03 22:15:49      uniqid c5bb3ed1d2110203e2850998f1008e92   IP 0.0.0.0   [email protected] StatusCode: 1000 Second named error has occurred
Dec 04 07:53:12      uniqid 476ceb443a552012a1dd1e754d279018   IP 255.255.255.255   [email protected] StatusCode: 9999 First named error has occurred
Dec 04 10:51:08 uniqid 4c27a6e016e8b5c0a659274d20c86e0c IP 192.168.0.1 [email protected] StatusCode: 0000 Another named error has occured

原始结果中的行数可能有很大差异

错误代码可能相差很大(但总是4位),并且全部在源文件中

错误代码的顺序可能会有所不同(除非行中有某种排序会更容易)。

全部通过RHEL bash完成

bash loops awk printf echo
1个回答
0
投票

根据问题,容量很大,可能很多行,等等。这意味着这里需要脚本引擎(awk,Perl,Python等)。两者的逻辑相同]

  • 为错误代码预加载查找表
  • 对于每一行:
    • 从第10列中查找错误代码
    • 将错误测试附加到行

使用awk,假设查找表在errors.txt中

awk -v LOOKUP=lookup.txt '
BEGIN {
   # Read the errors.txt file into array x, using custom  FS
   FS="=\\$"
   while (getline < LOOKUP) { x[$1] = substr($2, 2, length($2)-2) }
   # Restore FS
   FS=" "
}
{
   print $0, x["e" $10] ;
} data.txt

© www.soinside.com 2019 - 2024. All rights reserved.