Visit Our UNIX and Linux User Community


regex help to get unique hostnames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex help to get unique hostnames
# 1  
Old 09-13-2009
regex help to get unique hostnames

Hi

I have a file containing hostnames like this

Code:
(host=myhost.domain.com) or
(host=myhost)

i need to extarct the unique hostnames without the domain names from that file.
so my output should be myhost (without domain names)

But my regex skills are rusty

Code:
i tried grep "host" myfile|uniq

Code:
but this matches host=myhost.domain.com

instead of just everything after host=

any ideas on how do i construct my regex

Last edited by xiamin; 09-13-2009 at 04:31 AM..
# 2  
Old 09-13-2009
Code:
$ 
$ cat f1
host=myhost1.domain1.com
host=myhost2
host=myhost3.domain3.com
host=myhost1.domain1.com
host=myhost1.domain1.com
host=myhost2
host=myhost4
$ 
$ perl -nle 's/host=//g; print' f1 | sort | uniq
myhost1.domain1.com
myhost2
myhost3.domain3.com
myhost4
$ 
$

tyler_durden
# 3  
Old 09-13-2009
Code:
> cat hostfile
(host=myhost1.domain1.com)
(host=myhost2)
(host=myhost3.domain3.com)
(host=myhost1.domain1.com)
(host=myhost1.domain1.com)
(host=myhost2)
(host=myhost4)
> 
> 
> perl -nle 'if (/host=(.*?)(\.|\))/) {print $1}' hostfile | sort | uniq
myhost1
myhost2
myhost3
myhost4

# 4  
Old 09-13-2009
A awk one:

Code:
$ cat f
(host=myhost1.domain1.com)
(host=myhost2)
(host=myhost3.domain3.com)
(host=myhost1.domain1.com)
(host=myhost1.domain1.com)
(host=myhost2)
(host=myhost4)

$ awk -F"[=.)]" '{h[$2]=1}END{for(i in h) print i}' f
myhost1
myhost2
myhost3
myhost4

# 5  
Old 09-13-2009
Or:
Code:
$ awk -F"[=.)]" '!c[$2]++{print $2}' f


Previous Thread | Next Thread
Test Your Knowledge in Computers #67
Difficulty: Medium
IT security or cybersecurity controls are based on a combination of three types of security controls, (1) physical controls, (2) logical controls and (3) administrative controls.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to identify unique words in a dictionary database

Hello, I have a dictionary which I am building for the Open Source Community. The data structure is as under HEADWORD=PARTOFSPEECH=ENGLISH MEANING as shown in the example below अ=m=Prefix signifying negation. अँहँ=ind=Interjection expressing disapprobation. अं=int=An interjection... (2 Replies)
Discussion started by: gimley
2 Replies

2. Shell Programming and Scripting

Grouping hostnames for pdsh

Hi All, I have a list of hostnames in a text file and I would like to craft a bash script that would group them for use with pdsh. For example... I would like to group the following from a file: hostname1000 hostname1001 hostname1002 hostname1003 hostname1004 hostname2000... (4 Replies)
Discussion started by: joeg1484
4 Replies

3. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

4. Shell Programming and Scripting

Ip address for multiple hostnames

i have a file which has 100 servers,i want a script which can output me ip address and hostname for that server. Thanks in advance!! input file abc.com output file should be abc.com 192.168.1..1 (1 Reply)
Discussion started by: Moon1234
1 Replies

5. UNIX for Dummies Questions & Answers

Cyrillic Hostnames?

As the title suggests, I'm wondering if its possible to set a hostname to a name or word with Cyrillic letters (e.g. - Like the Russian alphabet). I tried installing the cyrillic-console package in Debian. I switched my desktop to Russian as well. However, when I try to set the hostname I get that... (2 Replies)
Discussion started by: Azrael
2 Replies

6. Programming

Getting All VM Hostnames Using Pysphere

I need to obtain a list of all the hostname of all the VM's that reside on two ESXI servers using pysphere. I have attempted using the basic methods but an unable to get a hostname in my testing using: from pysphere import VIServer server = VIServer() ... (0 Replies)
Discussion started by: metallica1973
0 Replies

7. UNIX for Dummies Questions & Answers

Print unique lines without sort or unique

I would like to print unique lines without sort or unique. Unfortunately the server I am working on does not have sort or unique. I have not been able to contact the administrator of the server to ask him to add it for several weeks. (7 Replies)
Discussion started by: cokedude
7 Replies

8. Shell Programming and Scripting

Change unique file names into new unique filenames

I have 84 files with the following names splitseqs.1, spliseqs.2 etc. and I want to change the .number to a unique filename. E.g. change splitseqs.1 into splitseqs.7114_1#24 and change spliseqs.2 into splitseqs.7067_2#4 So all the current file names are unique, so are the new file names.... (1 Reply)
Discussion started by: avonm
1 Replies

9. Shell Programming and Scripting

get part of file with unique & non-unique string

I have an archive file that holds a batch of statements. I would like to be able to extract a certain statement based on the unique customer # (ie. 123456). The end for each statement is noted by "ENDSTM". I can find the line number for the beginning of the statement section with sed. ... (5 Replies)
Discussion started by: andrewsc
5 Replies

10. IP Networking

DHCP and Hostnames

I have a linksys DSL router. I run a mixed environment of Windows, Linux and Solaris. The routers dhcp tables show the windows machines hostnames and associated IP's ok but the unix bases mahines don't have associated hostnames in the DHCP tables. I can ping by IP but not my the machines... (5 Replies)
Discussion started by: mr16ga
5 Replies

Featured Tech Videos