LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-08-2010, 06:42 AM   #1
rajesh1978
LQ Newbie
 
Registered: Jun 2010
Posts: 26

Rep: Reputation: 0
Talking Grep


Hi,
I have a very big text file. I want to find the line number and words ( not the complete line) that contains the pattern 'XY' like XYabc or abcXY or aXYbc etc. (XY is always together).

can we use grep for this
please help
 
Old 10-08-2010, 06:47 AM   #2
sohail0399
Member
 
Registered: Oct 2008
Location: Pakistan, Islamabad
Distribution: CentOS, Fedora, Solaris
Posts: 154

Rep: Reputation: 23
try grep -n word_to_serch file_name
 
Old 10-08-2010, 06:50 AM   #3
rajesh1978
LQ Newbie
 
Registered: Jun 2010
Posts: 26

Original Poster
Rep: Reputation: 0
I have tried that. It is giving the whole line. I require only the word containing the letter 'XY' with line number
 
Old 10-08-2010, 06:53 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

Is this what you are looking for:

egrep -no "[[:alpha:]]*XY[[:alpha:]]*" infile

Hope this helps.
 
Old 10-08-2010, 06:53 AM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Try this:

grep Pattern Filename | wc -w
 
Old 10-08-2010, 06:54 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Take a look at the -o option which shows only the matching part. However, if you grep only for the pattern "XY" it will show you only "XY", so that you have to elaborate a regular expression to match the entire words containing "XY".

Edit: druuna beated me. Just a note anyway: if the word can contain any character, like numbers or special characters, I would end-up with something like this:
Code:
grep -Eno '[^[:space:]]*XY[^[:space:]]*' file
to match anything but the space characters.

Last edited by colucix; 10-08-2010 at 06:57 AM.
 
Old 10-08-2010, 07:04 AM   #7
rajesh1978
LQ Newbie
 
Registered: Jun 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Druuna it worked
Thanks
 
Old 10-08-2010, 07:06 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Python alternative:

Code:
for line in open("file"):
    for word in line.split(): #split into each individual words
         if "XY" in word:
               print word
 
Old 10-08-2010, 07:40 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Quote:
Originally Posted by rajesh1978 View Post
Druuna it worked
Thanks
You're welcome
 
Old 10-08-2010, 08:27 AM   #10
rajesh1978
LQ Newbie
 
Registered: Jun 2010
Posts: 26

Original Poster
Rep: Reputation: 0
One more thing if that word contains digits and alpha then what I need to do

Last edited by rajesh1978; 10-08-2010 at 08:32 AM.
 
Old 10-08-2010, 08:43 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

That is already answered by hostdog74 in post #6. Here a slight variation on that answer:

egrep -no "[^[:space:]]*XY[^[:space:]]*" infile

Instead of looking for alphanumeric characters (my first solution), this excludes spaces of any kind.

Hope this helps.

Last edited by druuna; 10-08-2010 at 08:57 AM. Reason: fixed a typo
 
Old 10-08-2010, 08:51 AM   #12
rajesh1978
LQ Newbie
 
Registered: Jun 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Great!!!
It worked for any kind of characters ( For special character also)

Thanks a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 03:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration