Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-29-2012
Registered User
 
Join Date: May 2011
Posts: 94
Thanks: 25
Thanked 0 Times in 0 Posts
Grepping using -w and dashes (-)

I have a script to sort a list of arbitrary hosts and determine if they are supported by grepping them into a master supported list. I cut all the suffixes of the hosts in the arbitrary list, leaving the "short" hostname if you will, then grep -w them into the master list. For example:

hosta.something.com becomes hosta, and hosta is grepped with -w into the master list. The master list may return something like hosta.anything.internal.com which is fine because that's the suffix our supported list uses.

However, there are some hosts on our list named hosta-1, hosta-1-10, hosta-b-svr and so forth. When I grep -w hosta it will return all those others because the grep is not delimited by a dash. This seems like a particularly difficult problem because if the arbitrary list DOES supply a hostname with a dash I still need to be able to grep that.

I think this is an impossible situation but just thought I'd ask.
Sponsored Links
    #2  
Old 03-29-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
Maybe I'm missing something, but is there a reason that dropping the -w and adding leading "whitespace" and a trailing dot would not work? Assuming that shortname holds the converted host name that you're searching for:


Code:
#if pattern is expected  any place other than the beginning of the line
grep "[ \t]${shortname}\."  master_file

# if pattern is expected as first token on line:
grep "^${shortname}\."  master_file

If these won't do it, then please post a sample of your master file (with obvious names dummied out).
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
Removing columns with dashes Xterra Shell Programming and Scripting 14 07-07-2010 07:28 PM
AWK script to omit top characters with dashes magikminox Shell Programming and Scripting 3 09-02-2008 12:42 PM
grep throws in dashes? kingdbag UNIX for Dummies Questions & Answers 3 04-27-2007 04:18 PM
double dashes systemsb UNIX for Dummies Questions & Answers 2 05-18-2006 10:41 AM
help removing dashes from social security number Marcia P UNIX for Dummies Questions & Answers 2 02-28-2006 08:10 PM



All times are GMT -4. The time now is 02:45 AM.