简体   繁体   中英

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.

Below are the two command with i want to grep the data line by line and then put into a csv file .

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

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

您可以将awkgzat使用:

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

Use awk to format it output from 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).

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

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

Alternative (pure sed without grep):

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:

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

paste f1 f2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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