LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-04-2010, 06:44 AM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 49

Rep: Reputation: 1
Question searching pattern in tab delimited file using grep


Suppose i have a file(1.txt) separated by TAB delimiter in a line

1 B AB 2
2 C AB 2

if I need to search for the records having B?? using grep
If i need to perform multiple search like line having "C and AB" or "B and AB"??

simple grep 'B' 1.text gives both record.

Please help
Thanks in advance
Vaibhav
 
Old 03-04-2010, 06:48 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Use grep's -w option: grep -w B 1.txt

-w -> Select only those lines containing matches that form whole words.

Hope this helps.
 
1 members found this post helpful.
Old 03-04-2010, 07:18 AM   #3
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 49

Original Poster
Rep: Reputation: 1
Hi,

What about 2nd question?

If i need to perform multiple search like line having "C and AB" or "B and AB"??

Thanks,
Vaibhav
 
Old 03-04-2010, 07:23 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You can use a pipe to connect two grep commands.
 
Old 03-04-2010, 07:25 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

What do you actually want to search for: C<tab>AB or a C and AB anywhere in a line (AB X Y C would be an example of the second)?

Assuming you want the first:
grep "C<tab>AB" infile, there is one catch: pressing the tab will tell bash to try to complete the C. You can enter a tab in the search string by first pressing <ctrl-v> followed by pressing the <tab>.
 
Old 03-05-2010, 01:02 AM   #6
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 49

Original Poster
Rep: Reputation: 1
Cool This is absolutely correct

Quote:
Originally Posted by druuna View Post
Hi,

Use grep's -w option: grep -w B 1.txt

-w -> Select only those lines containing matches that form whole words.

Hope this helps.

Thanks.
It Worked!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM
Working with Tab-Delimited files shiroh_1982 Linux - Newbie 5 06-02-2006 04:08 AM
pattern searching - kindly help hinetvenkat Linux - Software 2 08-08-2005 11:19 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 10:34 AM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration