1

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

7
  • 3
    You mean zgrep -E 'Time|requests' file.txt.gz > file.csv? Commented Apr 28, 2016 at 7:59
  • Is file.txt.gz a single file or you need to recursively grep inside that directory? Commented Apr 28, 2016 at 8:01
  • yes this is single file Commented Apr 28, 2016 at 8:02
  • @anubhava this is working but i want both the output as pipe seprated like TimeOutput | RequestOut\ Commented Apr 28, 2016 at 8:06
  • I suggest you show your expected output as there can be multiple matches of each word in the file. Commented Apr 28, 2016 at 8:10

4 Answers 4

3

You can use awk with gzat:

gzcat file.txt.gz | awk '/Time/{p=$0} /requests/{print p, "|", $0}'
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm in that case you will need to gunzip the file first using gunzip
is it gzat or gzcat?
@J.Chomel: gcat is gnu utility but on OSX we get gzcat
1

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 ,

1 Comment

where is the file name ??
0

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*$//'

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.