2

I have a .gz file with contents like the following:

ID:123455   23-07-2015 mvni
warning: RTM post
warning : no profile data

Using the string no profile data, I want to print the ID. I tried with grep before and after lines but it's not working. Please suggest any other methods.

4 Answers 4

3
$ zgrep -B2 'warning : no profile data' *.gz | grep -o '^ID:[0-9]*'
ID:123455

As pointed out by user3188445, zgrep will grep for strings within (possibly compressed) files. Using -B2 to print 2 lines before the warning match, then extracting the IDs of all matching files using a standard grep against stdout.

This will work whether you have several compressed files, or several matching sections within the same file.

4
  • i tried using this command grep -B2 'warning :no profile data'* .gz but its not working .telling -B does not exist Commented Jul 23, 2015 at 15:03
  • In that case, I would suggest going with the second update of user3188445's answer, which should work with older versions of zgrep as well. Commented Jul 23, 2015 at 15:15
  • if i have to print only the number of ID . Means from ID:123455. I just want to print 123455 Commented Jul 23, 2015 at 15:18
  • zgrep -l 'warning : no profile data' *.gz | xargs zgrep '^ID:' | sed -e 's/^ID:\([0-9]*\) .*/\1/', as suggested by user3188445. That will work with your version of zgrep. Commented Jul 23, 2015 at 15:22
3

The zcat command or gzip -dc will uncompress and print the output of a gzipped file to stdout. So you can run, for example, zcat file.gz | grep '^ID:'. However, most systems have a command called zgrep that already does that for you.

update

Under the assumption that you have a bunch of these files, and want to print the ID line from files that contain a particular warning, you can do this:

zgrep -l 'warning : no profile data' *.gz | xargs zgrep '^ID:'

The first command, zgrep -l, prints a list of files that contain the warning. The second command, xargs, takes a list of arguments on standard input and runs a command on all the inputs. The command it runs is also zgrep, so as to print the ID line you want.

Second update

To extract just the numeric ID, take the command I previously suggested and append

| sed -e 's/^ID:\([0-9]*\) .*/\1/'

That will just print the ID number.

20
  • i want to search first the keyword 'no profile data' then only to print the ID Commented Jul 23, 2015 at 14:57
  • Can you elaborate? Do you have a bunch of these files, and you want to print the ID line from the files that have a warning? Or does the file have multiple sections? Commented Jul 23, 2015 at 14:58
  • i am having bunch of these files . In these files where the warning come as no profile data . I have to print only the first section that is ID:123455 Commented Jul 23, 2015 at 15:01
  • I assume by first section you mean first line? If so, then does my update work? Commented Jul 23, 2015 at 15:05
  • in the first line , i need only the ID Commented Jul 23, 2015 at 15:07
1

You can use zgrep to grep a .gz file. I suspect you want something like:

zgrep -B 2 'warning : no profile data' file.gz
3
  • i tried this but its not working .Its telling that -B does not exist Commented Jul 23, 2015 at 15:06
  • Ah, perhaps your copy of zgrep is a little too simplistic. In which case, you might want to try: zcat file.gz | grep -B 2 'warning : no profile data' Commented Jul 23, 2015 at 15:07
  • No , its not working -B options invalid Commented Jul 24, 2015 at 2:27
1

If you have access to GNU utilities, this should work:

zgrep -B2 "no profile data" file | grep -oP 'ID:\K\d+'

If that doesn't work, you can try this instead:

zcat file.gz | grep -B2 "no profile data" | sed -n 's/ID:\([0-9]*\).*/\1/p'

Or:

zcat file.gz | 
 awk '{if(/^ID/){split($1,a,/:/); id=a[2];}if(/no profile data/){print id}}'

Or:

 zcat file.gz | perl -lne '$id=$1 if /^ID:(\d+)/; print $id if /no profile data/'
2
  • The contents of my .gz file is as below ----------------------------------------------------------------------------- ID:342N000390AAAAAAAA 07/14/15 10:26 (MV90 ) * Warning - No Profile Data * Register Data Imported 07/14/15 10:24 05/13/15 08:16 15 1 5956 --------------------------------------------------------------------- Commented Jul 24, 2015 at 2:31
  • i tried what you told but its not working Commented Jul 24, 2015 at 2:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.