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 11-27-2011, 09:27 PM   #1
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Rep: Reputation: Disabled
automatic substring matcher


Hello,

I am trying to determine if a substring of a line, in a file, matches to other substrings in other lines of the same file.

For ex, this is what my file looks like:

col 1 |col2 | col3
GO 3 DOG3332-TB
SJ 2.5 DOG3554-TA
NO 2 DOG3332-TB
SO 1 DOG3332-TA

My substring would be DOG*(and the exact numbers after). How can I automate this so that the numbers after the word DOG are matched and then removed automatically?

Basically, if the numbers following "DOG" appear more than once remove them.
 
Old 11-28-2011, 03:56 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
If you want to remove the whole line, a simple awk code should do the trick
Code:
awk 'NR > 1 { string = gensub(/(DOG[0-9]+).*/,"\\1",1,$3); if ( ! _[string]++ ) print}' file
otherwise it should be simple to slightly modify this one-liner if you want to remove only a part of the line (this point is not clear from your question). Hope this helps.
 
Old 11-29-2011, 11:39 AM   #3
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks, and if I wanted to know if the word DOG* appears in col 1, how many times will that same word DOG* appeared in col 2?
 
  


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
Extract a substring prpersonal Linux - General 8 10-19-2009 03:44 PM
How to substring santhoshv Linux - Newbie 3 09-09-2008 06:09 AM
Problem with Gnucash import transaction matcher mma8x Linux - Software 1 03-02-2008 04:01 AM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 12:31 PM
to get a substring dimah Programming 1 06-22-2006 07:31 AM


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