一起使用echo和grep

时间:2015-03-02 16:46:51

标签: linux bash unix

我想一起使用echo和grep语句。我已经尝试了大部分但无法得到确切的输出 我想要的。

     aa=$(grep -A100000 "2010-03-24" log.txt|grep "ORA")
     echo "Ip-Address|Directory Name|${aa}" > output.txt

我正在考虑日期,因为我想要在当前日期之后的所有行,然后grep" ORA"从中。还有其他方法,但根据我的日志文件,这是最合适的方式。 我得到这样的输出。

     10.46.162.86|ASD----Exception|2010-03-24 07 ORA-00001 - 80 -                  
     173.45.230.59 
     2010-03-24 07:00:47 ORA-00942 - 80 - 173.45.230.59
     2010-03-24 07:01:15 ORA-00001 - 80 - 173.45.230.59
     2010-03-24 07:02:17 ORA-12849 - 80 - 173.45.230.59
     2010-03-24 07:05:09 ORA-00001 - 80 - 173.45.230.59

理想的输出应该是

     10.46.162.86|ASD----Exception|2010-03-24 07 ORA-00001 - 80 -  
     173.45.230.59 
     10.46.162.86|ASD----Exception|2010-03-24 07:00:47 ORA-00942 - 80 -    
     173.45.230.59
     10.46.162.86|ASD----Exception|2010-03-24 07:01:15 ORA-00001 - 80 - 
     173.45.230.59
     10.46.162.86|ASD----Exception|2010-03-24 07:02:17 ORA-12849 - 80 - 
     173.45.230.59
     10.46.162.86|ASD----Exception|2010-03-24 07:05:09 ORA-00001 - 80 -  
     173.45.230.59

我从不同目录的日志文件中获取ORA。

输入就像

     2010-03-22 07:00:47 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00001 -  
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en-   
     US;+rv:1.9.2.2)
     2010-03-22 07:00:47 ZZZZC941948879 RUFFLES 222.222.222.222 GET  
     /2009/10/yep-twitter-down.ht
     2010-03-22 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 GET  
     /img/input-bg.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+ 
     (Windows;+U;+Windows+NT+9.0;+en-  
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-23 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00001 -      
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en- 
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-23 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 GET  
     /img/topnav-about.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+ 
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319
     2010-03-23 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 GET     
     /img/entry-hr.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+      
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319+Firefox
     2010-03-23 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00001 -   
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en-   
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-24 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 GET    
     /img/header-bg.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+    
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319
     2010-03-24 07:00:48 ZZZZC941948879 RUFFLES 222.222.222.222 GET     
     /img/bullet.gif - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+ 
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319+Firefox
     2010-03-24 07:00:49 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00001 -                   
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en- 
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-24 07:00:49 ZZZZC941948879 RUFFLES 222.222.222.222 GET /img/bg-        
     module.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+  
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319
     2010-03-24 07:00:50 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00942 -      
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en- 
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-24 07:00:50 ZZZZC941948879 RUFFLES 222.222.222.222 GET /img/bg-    
     sidebarul.jpg - 80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+ 
     (Windows;+U;+Windows+NT+9.0;+en-US;+rv:1.9.2.2)+Gecko/20100319
     2010-03-24 07:00:50 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00001 -  
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en-   
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2
     2010-03-24 07:00:51 ZZZZC941948879 RUFFLES 222.222.222.222 ORA-00942 -      
     80 - 98.88.35.133 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+9.0;+en- 
     US;+rv:1.9.2.2)+Gecko/20100319+Firefox/3.9.2

这里的问题是当我在进行grep操作时,根据异常提取100行或更多行,并且我能够将Ip-Address和节点名称仅附加到一行。 此外,IP地址和节点名称是在运行时生成的。 请建议一种获得所需输出的方法。 感谢。

2 个答案:

答案 0 :(得分:2)

由于我只是知道特殊字符会出现在目录名称中,所以我更喜欢使用awk over sed来避免代码注入问题:

grep -A100000 "2010-03-24" log.txt | awk -v prefix="IP-Address|Directory name|" '/ORA/ { print prefix $0 }' > output.txt

相关部分是

awk -v prefix="IP-Address|Directory name|" '/ORA/ { print prefix $0 }'

使用-v prefix=value,awk知道具有给定值的名为prefix的变量,/ORA/ { print prefix $0 }指示awk处理与正则表达式ORA匹配的所有行打印prefix后跟行($0)。

答案 1 :(得分:1)

@etanreisner给了你答案。

一种方式:

尝试:

grep -A100000 "2010-03-24" log.txt|grep "ORA" |
while read aa
do
   echo "Ip-Address|Directory Name|${aa}"
done > output.txt