简体   繁体   English

Grep字符串并放入CSV文件Unix

[英]Grep string and put in csv file Unix

I have gz file that have some data now i want to grep two diffrent pattern and put that data into a csv file .For the same i want to write a shell script how can we do this please help me in this. 我有一些数据的gz文件,现在我想grep两个不同的模式,并将该数据放入一个csv文件中。同样,我想编写一个shell脚本,我们该怎么做,请帮助我。

Below are the two command with i want to grep the data line by line and then put into a csv file . 下面是我要逐行grep然后将其放入csv文件的两个命令。

Commands : 命令:

zgrep "Time"  file.txt.gz
zgrep "requests" file.txt.gz

Please suggest how could i use these command in shell and get the data in a csv file 请建议我如何在外壳中使用这些命令并在CSV文件中获取数据

This is the output i am getting after doing : 这是我做完后得到的输出:

zgrep -E 'Time|requests' file.txt.gz

 Time 27-Apr-2016 07:24:15 CDT,
 requests currently being processed, 1 
  Time 27-Apr-2016 07:24:15 CDT,
 requests currently being processed, 2 ,

I want the ouput like Time 27-Apr-2016 07:24:15 CDT | requests currently being processed, 1 我想要类似Time 27-Apr-2016 07:24:15 CDT | requests currently being processed, 1的输出Time 27-Apr-2016 07:24:15 CDT | requests currently being processed, 1 Time 27-Apr-2016 07:24:15 CDT | requests currently being processed, 1

您可以将awkgzat使用:

gzcat file.txt.gz | awk '/Time/{p=$0} /requests/{print p, "|", $0}'

Use awk to format it output from grep: 使用awk格式化它从grep输出:

zgrep -E ... | awk 'NR%2==0{print l, "|", $0}{l=$0}'
 Time 27-Apr-2016 07:24:15 CDT, |  requests currently being processed, 1 
  Time 27-Apr-2016 07:24:15 CDT, |  requests currently being processed, 2 ,

I assume, that 'Time ...' and ;requests ...' in grep output are on one line (not wrapped as in your example). 我假设grep输出中的'Time ...'和; requests ...'在一行上(没有像您的示例中那样包装)。

zgrep -E 'Time|requests' file.txt.gz | sed -r -e 's/\\s*,\\s*/ | /' > file.csv

To remove trailing comma as in 'being processed, 2 ,' use this variant 要删除'being processed, 2 ,'中的尾部逗号'being processed, 2 ,'使用此变体

zgrep -E 'Time|requests' file.txt.gz | sed -r -e 's/\\s*,\\s*/ | /; s/\\s*,\\s*$//'

Alternative (pure sed without grep): 替代方法(不带grep的纯sed):

gzip -dc file.txt.gz | sed -r -e '/Time|requests/!d; s/\\s*,\\s*/ | /; s/\\s*,\\s*$//'

You could also use paste to do so: 您也可以使用paste操作:

zgrep -E 'Time'     file.txt.gz >f1
zgrep -E 'requests' file.txt.gz >f2

paste f1 f2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM