LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-04-2010, 06:44 AM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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: 51

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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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: 51

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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 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
Open Source Consulting | Domain Registration