How to grep this pattern?? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to grep this pattern??

Shell Programming and Scripting


Tags
solaris

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-19-2008
askumarece askumarece is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 14 July 2009, 7:56 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
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

Last edited by askumarece; 02-19-2008 at 01:22 PM..
Sponsored Links
    #2  
Old 02-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 October 2014, 11:30 PM EDT
Location: NM
Posts: 10,253
Thanks: 285
Thanked 803 Times in 747 Posts
I must be missing something:

Code:
grep 'package com.att.com;' file

does what you ask? It will find 'package com.att.com;' anywhere in the line of text.
Sponsored Links
    #3  
Old 02-19-2008
askumarece askumarece is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 14 July 2009, 7:56 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
I must be missing something:

Code:
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.....

Last edited by askumarece; 02-19-2008 at 01:26 PM..
    #4  
Old 02-19-2008
Lukeadams Lukeadams is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 10 November 2008, 4:32 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #5  
Old 02-19-2008
askumarece askumarece is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 14 July 2009, 7:56 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
I want to grep for the lines w/ spaces before the words. Can you give me the grep command for this?
Sponsored Links
    #6  
Old 02-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 October 2014, 11:30 PM EDT
Location: NM
Posts: 10,253
Thanks: 285
Thanked 803 Times in 747 Posts

Code:
# 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.
The Following User Says Thank You to jim mcnamara For This Useful Post:
crimso (11-12-2010)
Sponsored Links
    #7  
Old 02-19-2008
askumarece askumarece is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 14 July 2009, 7:56 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
jim,
thanks a lot.... sorry for making u in confusion....
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep pattern alok.behria UNIX for Dummies Questions & Answers 2 05-05-2011 01:34 PM
Want to grep for a pattern and display the content above that pattern ajayakunuri Shell Programming and Scripting 6 06-25-2010 06:48 AM
How to grep for certain pattern aoussenko Shell Programming and Scripting 4 09-15-2009 07:37 PM
grep for pattern aemunathan Shell Programming and Scripting 12 05-09-2008 02:58 AM
help with grep the pattern bluemoon1 Shell Programming and Scripting 2 09-20-2007 11:27 AM



All times are GMT -4. The time now is 01:32 AM.