Grepping using -w and dashes (-)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grepping using -w and dashes (-)
# 1  
Old 03-30-2012
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.
# 2  
Old 03-30-2012
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).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Mail command not working for Long Email Address with Dashes

My company has an email user group email address name that has 35 characters in front of the @ symbol where two of them are dashes. For some reason the mail command fails to send email to this address when I invoke it from the Linux command line. I don't understand the reason for the failure. Below... (5 Replies)
Discussion started by: Phil44
5 Replies

2. UNIX for Dummies Questions & Answers

Filling tab space with dots or dashes

Hey Guys & Gals ! My script is creating a numbered list and I would like to have the numers and results spaced apart by a tab. Now to make sure it is clear which number corresonds with which outcome, I would like to have the tab space filled with dashes. Anyone able to tell me if this... (6 Replies)
Discussion started by: TAPE
6 Replies

3. Shell Programming and Scripting

Remove dashes if exist between positions 351-357 and then add - at the 357th position

I need to remove dash (i.e. -) if present from positions 351-357, and then add - (dash) at 357th position. For example in following first and 3rd record we got Before processing 1) 1st Record positions 351-357 = 0-12345 2) 3rd Record positions 351-357 = 00-4567 After processing:- 1) 1st... (7 Replies)
Discussion started by: lancesunny
7 Replies

4. Shell Programming and Scripting

For loop with dashes in filenames causing weird output

Hi again, What i'm trying to accomplish here is search a large directory for certain filesames, read from a txt file and looping through it. For instance, one of my target names from the loop file is: 1ad55f47-c342-496b-a46d-ba7de0f1b434 My loop is constructed thusly, run in a directory... (2 Replies)
Discussion started by: Karunamon
2 Replies

5. Shell Programming and Scripting

Removing columns with dashes

My files look like this I need to remove the columns where dashes are the majority, if any of the sequences has any character in that particular position it should be removed too. The IDs and Freqs should be kept intact. Thus, the resulting file should look like this Thanks in advance (14 Replies)
Discussion started by: Xterra
14 Replies

6. Shell Programming and Scripting

AWK script to omit top characters with dashes

Hi I have an AWK script that takes an input file and outputs it as CSV format. The problem is its also outputting the characters at the top which are dashes(-------) and i want it to leave them out. My script is as follows. BEGIN { count=1; } /^/ { count+=1 if ( count > 2 ){... (3 Replies)
Discussion started by: magikminox
3 Replies

7. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

8. UNIX for Dummies Questions & Answers

grep throws in dashes?

Hey guys, I'm trying to grep for two things out of a file and I got that working but why is it randomly throwing "--" in the output? Is there a simple way to get rid of them? It only seems to do it when the line above what im looking for has numbers in it. $ egrep -i -B 1... (3 Replies)
Discussion started by: kingdbag
3 Replies

9. UNIX for Dummies Questions & Answers

double dashes

function date_diff { integer _dt1=$(date_to_num ${1:?}) integer _dt2=$(date_to_num ${2:?}) integer _ndt=0 if ] then ((_ndt=_dt2-_dt1)) fi print -- $_ndt } What does double dash '--' indicate ? Can anyone please answer this (2 Replies)
Discussion started by: systemsb
2 Replies

10. UNIX for Dummies Questions & Answers

help removing dashes from social security number

I have a file containing social security numbers with the format ###-##-####. I need to read each record in this file, reformat the SSN to the format #########, and write the record with the reformatted SSN to a new file. I am a UNIX newbie. I think I need to use either the sed or awk commands, but... (2 Replies)
Discussion started by: Marcia P
2 Replies
Login or Register to Ask a Question