Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
我想在bash脚本的输出中添加错误的含义_Bash_Loops_Awk_Printf_Echo - Fatal编程技术网

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

我想在bash脚本的输出中添加错误的含义,bash,loops,awk,printf,echo,Bash,Loops,Awk,Printf,Echo,我有一个源文件,里面填充了这样的行 e9999=$"First named error has occurred" e1000=$"Second named error has occurred" e0000=$"Another named error has occurred" 我有一个脚本,它以这样的行输出结束。脚本是多个greps到一些sed,然后是更多greps Dec 03 22:15:49 uniqid c5bb3ed1d2110203e2850998f1008e92

我有一个源文件,里面填充了这样的行

e9999=$"First named error has occurred"
e1000=$"Second named error has occurred"
e0000=$"Another named error has occurred"
我有一个脚本,它以这样的行输出结束。脚本是多个greps到一些sed,然后是更多greps

Dec 03 22:15:49      uniqid c5bb3ed1d2110203e2850998f1008e92   IP 0.0.0.0   email@email.fqdn StatusCode: 1000
Dec 04 07:53:12      uniqid 476ceb443a552012a1dd1e754d279018   IP 255.255.255.255   email@email.fqdn StatusCode: 9999
Dec 04 10:51:08      uniqid 4c27a6e016e8b5c0a659274d20c86e0c   IP 192.168.0.1   email@email.fqdn StatusCode: 0000
我希望在输出的末尾添加每行“statuscode”的含义

例如:

原始结果中的行数可能会有很大的变化

错误代码可能变化很大,但始终为4位数字,并且都在源文件中

错误代码的顺序可能会有所不同,除非行的排序使其更容易

这一切都是通过RHEL bash完成的 **样本输入:**

Dec 05 19:20:46应用程序[debug][session 75b49d52fdd572752c2848f9992cb838][IP 255.255.255.255]通信:消息:公司密码传递测试app18b4e79f-ee3c-4a15-91f0-f97edb15478cPassDelivery app4d82fb84-bffb-4b48-aea1-98a60236fd482019-12-05T19:20:45ZCallPlaced7000 Dec 05 19:20:46应用程序[debug][session 75b49d52fdd572752c2848f9992cb838][IP 255.255.255.255]reg_phase1_info_请求。p:正在进行呼叫以获取email@e.com状态代码:7000 Dec 05 19:20:56应用程序[debug][session 75B49D52FDD572752C2848F9992C838][IP 255.255.255.255]通信:消息:4d82fb84-bffb-4b48-aea1-98a60236fd482019-12-05T19:20:56Z7000 Dec 05 19:20:56应用程序[debug][session 75b49d52fdd572752c2848f9992cb838][IP 255.255.255.255]reg_phase1_info_请求。p:正在呼叫email@e.com状态代码:3340 Dec 05 19:21:06应用程序[调试][会话75B49D52FDD572752C2848F9992C838][IP 255.255.255.255]通信:消息:4d82fb84-bffb-4b48-aea1-98a60236fd482019-12-05T19:21:06Z7000 Dec 05 19:21:06应用程序[debug][session 75B49D52FDD572752C2848F9992C838][IP 255.255.255.255]reg_phase1_info_请求。p:正在呼叫email@e.com状态代码:3130 Dec 05 19:26:28应用程序[debug][session 75b49d52fdd572752c2848f9992cb838][IP 255.255.255.255]通信:消息:公司密码传递测试app18b4e79f-ee3c-4a15-91f0-f97edb15478cPassDelivery app4d82fb84-bffb-4b48-aea1-98a60236fd482019-12-05T19:26:28ZPhoneSessionDeliver3370 Dec 05 19:26:28应用程序[debug][session 75b49d52fdd572752c2848f9992cb838][IP 255.255.255.255]reg_phase1_info_请求。p:的状态未知email@e.com状态代码:3370 Dec 05 19:26:28应用程序[debug][session 75B49D52FDD572752C2848F9992C838][IP 255.255.255.255]reg_phase1_info_请求。p:呼叫结束,消息类型:结果。状态代码:3013

**代码示例:**

#!/bin/bash
source ~/ondemandscripts/errorcodes
today=$(date "+%b %d")
yesterday=$(date -d "1 day ago" "+%b %d")
echo -e "\nChecking "/opt/app/app/logs/PHP_Debug_Log" for  errors and users email address"
echo -e "\n"


if [ $# -lt 1 ]
then
        echo -e "Designed for app\nUsage options:\n$0 all\n$0 today\n$0 yesterday\n$0 alluniq\n$0 todayuniq\n$0 yesterdayuniq\n"
                        exit 1;
                        fi
                        if [ $1 = "all" ]; then
                        grep -i 'statusCode' /opt/app/app/logs/PHP_Debug_Log | grep '1010\|1020\|2120\|2140\|2142\|2160\|2510\|2520\|2530\|2540\|2560\|3101\|3102\|3103\|3104\|3110\|3111\|3118\|3120\|3130\|3150\|3180\|3210\|3220\|3230\|3240\|3250\|3320\|3325\|3330\|3340\|3350\|3360\|3420\|3450\|3510\|3520\|3610\|3612\|3614\|3620\|3622\|3630\|3650\|3657\|3660\|3662' | sed 's/Comms.*<status>/  /' | sed 's/<\/statusCode>.*/ /' | sed 's/debug//'| sed 's/app//'| sed 's/>/ /' | sed 's/</ /' | sed 's/]/ /g' | sed 's/\[/ /g' | sed 's/reg_phase1.*result./ /' | sed 's/reg_phase1.*for / /' | grep -v "StatusCode: 7000" | grep 'email.com'

else
                               echo -e "Designed for app\nUsage options:\n$0 all\n$0 today\n$0 yesterday\n$0 alluniq\n$0 todayuniq\n$0 yesterdayuniq\n"

                                        fi

基于这个问题,卷非常大,可能是行,等等。这意味着这里需要脚本引擎awk、Perl、Python等等。。。。不管怎样,逻辑都是一样的

预加载查找表以查找错误代码 对于每一行: 从第10列中查找错误代码 将错误测试附加到he行 使用awk时,假设查找表位于errors.txt中

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


这一点也不清楚。请用三件重要的事情编辑你的问题。第一个输入示例,第二个输出示例,第三个是您为解决自己的问题所付出的努力,请编辑您的问题,然后告诉我们。您解决问题的代码在哪里,您的问题在哪里?@EdMorton您能否分享在这种情况下会使getline陷入循环的具体输入?上述用法在案例C的“应用”部分中描述为合适的解决方案@EdMorton Good point。我已经更新了答案。不知何故,我错过了awk手册中的这一段,并假设结果为0/1。谢谢,终于有时间添加此内容,并根据需要进行操作
awk -v LOOKUP=lookup.txt '
BEGIN {
   # Read the errors.txt file into array x, using custom  FS
   FS="=\\$"
   while ( (getline < LOOKUP) > 0 ) { x[$1] = substr($2, 2, length($2)-2) }
   # Restore FS
   FS=" "
}
{
   print $0, x["e" $10] ;
} 
' data.txt