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
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.
# 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.
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
------------------------
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......
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>
----
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
------------------------