How to grep this pattern??

Hi All,
I have a file like this, from this file I want to grep only this "package com.att.com;".... even though spaces are present at starting of the line.

i.e., the o/p should ignore these two lines from the file :

[space] [space] asdasdasd package ; ignore this line
[space] asdasdasd adsfkl90-9-0 package c.com; ignore this line also

Here it is the file content :
------------------------
package com.att.com;
[space] package com.att.com;
[double space] package com.att.com;
[space] package com.att.com;
[space] [space] package com.att.com;

package com.att.com;

package com.att.com;
package com.att.com;
package com.att.com;

[space] [space] asdasdasd package ; ignore this line
[space] asdasdasd adsfkl90-9-0 package c.com; ignore this line also

package com.att.com;
------------------------
thanks,
saravana

I must be missing something:

grep 'package com.att.com;' file

does what you ask? It will find 'package com.att.com;' anywhere in the line of text.

Jim,
But it will miss the lines which are having spaces b4 "package com.att.com;" I need to grep that also.....

I think you just need to add the "beginning of a line" character to only grep the lines that have no spaces before them.

grep '^package com.att.com;' file

Works in Solaris.

Your wording is a little confusing... normally when grep is used as a verb, at least in my experience, it means "get the lines that include". So if I want to find the lines in a dcoument that has the word foo them, I say "I want to grep for foo"

So... do you want to grep for the lines w/ spaces before the words or only those without spaces before the words?

Do include in the output the lines w/ spaces before, use the first example provided by Jim. If you want to exclude the lines w/ spaces before the text from your output, use the one I provided.

Cheers.

I want to grep for the lines w/ spaces before the words. Can you give me the grep command for this?

# one space in front of it anywhere on the line
grep ' package com.att.com;' file
# one or more white spaces
grep '[ \t]*package com.att.com;' file
# at the beginning of the line
grep '^ package com.att.com;' file
# one or more spaces at the beginning
grep '^[ \t]*package com.att.com;' file
# one or more spaces where it is at the end of the line
grep '[ \t]*package com.att.com;$' file

You need to be ultra-specific in asking for what you want.

1 Like

jim,
thanks a lot.... sorry for making u in confusion....

Jim,
Is it possible to get zero or more white spaces b4 the pattern??

Bez i have a file which looks like this.......
File Content :
---------------
package com.att.iom; line 1
[single space]package com.att.iom; line 2
[double space]package com.att.iom; line 3
package com.att.iom; line 4
something package com.att.iom;..... ##but I don't need this line line 5
[single space]package com.att.iom; line 6
something something package com.att.iom;##but I don't need this line line 7
package com.att.iom; line8
--------------------------

I need the o/p like this :
-----------------------
package com.att.iom; line 1
[single space]package com.att.iom; line 2
[double space]package com.att.iom; line 3
package com.att.iom; line 4
[single space]package com.att.iom; line 6
package com.att.iom; line8
------------------------

If it is possible then pls help me out........

thanks,
Saravana

In jim's last post, there are 5 grep solutions. Did you try using those ? One of those does what you want.

Yeah I tried.... But it is ignoring the lines which are being stored with tab insteadof space.....
for eg:
if i have the following lines in my file :
package com.att.iom; line 1
[space][space]package com.att.iom; line 2
[space] package com.att.iom; line 3
[space][space][space][space] package com.att.iom; line 4
something package com.att.iom;..... ##but I don't need this line line 5
[tab][tab]package com.att.iom; line 6 Here No space at begining of the line
something something package com.att.iom;##but I don't need this line line 7
package com.att.iom; line8

here is the o/p while I run this grep cmd:
---------------------------------
sa156s@::/export/home/sa156s> grep '^[ \t]*package com.att.iom;' file
package com.att.iom; line 1
package com.att.iom; line 2
package com.att.iom; line 3
package com.att.iom; line 4
package com.att.iom; line8
---------------------------------
it is ignoring the line #:6, but how to grep including that one also......

Saravana

That works well for me. Try escaping the backslash.

grep '^[ \\t]*package com.att.iom;' file

  1. \t ↩ī¸Ž

Vino,
see the o/p : Which I got line #6 is not there.... How to grep including the lines which starts with [tab]....
O/P:
----
sa156s@::/export/home/sa156s> grep '^[ \\t]*package com.att.iom;' file
package com.att.iom; line 1
package com.att.iom; line 2
package com.att.iom; line 3
package com.att.iom; line 4
package com.att.iom; line8
sa156s@::/export/home/sa156s>
----

Saravana

On that file, can you do the following ?

head -6 file | tail -1 | od -a

where the -6 means the 6th line. This will show us what the 6th line contains.

This is the file contents :
------------------------
package com.att.iom; line 1
[space][space]package com.att.iom; line 2
[space] package com.att.iom; line 3
[space][space][space][space] package com.att.iom; line 4
something package com.att.iom;..... ##but I don't need this line line 5
[tab][tab]package com.att.iom; line 6 Here No space at begining of the line
something something package com.att.iom;##but I don't need this line line 7
package com.att.iom; line8
------------------------