Developer Developer - 7 months ago 25
Bash Question

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

Answer

You can use awk with gzat:

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