Removing domain suffix with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing domain suffix with SED
# 1  
Old 03-31-2010
Removing domain suffix with SED

Hi Experts,

I have a syslog file from 1000's of different hosts which I want to adjust by removing the domain suffix from the hosts.

My previous attempts haven't managed to match all the different lenghts of the subdomains which are being logged.

Could somebody suggest which sed syntax could be used to accomplish this.

The domains list from 1 sub domain to 5 with ranging lengths of subdomain names ie:

host.i.want.to.have.this.removed.com
host.please.remove.me

Any assistance here would be appreciated.

Best Regards,

K
# 2  
Old 03-31-2010
Code:
awk -F. '{print $1}' filename


Last edited by radoulov; 03-31-2010 at 05:28 AM.. Reason: Please use code tags!
# 3  
Old 03-31-2010
Hi Ktrimu,

thanks for the quick reply here.

Using awk this does indeed remove the rest of the suffix but also the rest of the syslog after the end of the hostname / domain.

I actually want to remove just the domain suffix extension leaving the rest of the log intact.

Apologies if I didn't state this clearly in the earlier post.

Regards,

K
# 4  
Old 03-31-2010
It's probably a simple problem for a sed guru, if only they knew what the line looked like. Always provide a sample of the input you want people to help you with, and don't forget to mention any special cases if there are any.

Alister
# 5  
Old 03-31-2010
As i don't know the intricacies of the actual file here are some suggestions:

Code:
sed 's/ \([^ .]*\)\.[^ ]* / \1 /g'

This will replace any string of non-spaces/non-full-stops, followed by a full stop, followed by a series of non-spaces by the part up to the first full stop:

Code:
abc.def.ghi.jkl -> abc

In the regexp there are surrounding spaces to split on word boundaries, therefore the regexp will not work with "words" at the beginning or the end of a line. Furthermore, if there are "words" with full stops in them they would be shortened too (like in "my.name@emailadress.com" -> "my").

To catch FQDNs at line beginnings or endings in the same way you could add two more regexps:

Code:
sed 's/^\([^ .]*\)\.[^ ]* /\1 /
     s/ \([^ .]*\)\.[^ ]*$/ \1/'

If you can limit the valid characters for domain names any further you can of course fine-tune this mechanism to catch less non-domainnames. To adress my example of the email-address above, for instance:

Code:
sed 's/ \([^ @.]*\)\.[^ @]* / \1 /g'

This would leave email addresses untouched, because "@" is no longer considered a valid character in an FQDN.

I hope this helps.

bakunin
# 6  
Old 03-31-2010
MySQL

maybe like this

Code:
[root@rhnserver ~]# cat domain
cmachine.domain.com
xmachine.xdomain.domain.com
ymachine.subdomain.ydomain.domain.com
wmachine23.subsubdomain.subdomain.wdomain.domain.com
zmachine23.subsubbdomain.subbbdomain.subadomain.zdomaincom.domain.com

Code:
[root@rhnserver ~]# sed 's/\([[:alnum:]][[:alnum:]]*\)\.\([[:graph:]][[:graph:]]*\)/\1/g' domain
cmachine
xmachine
ymachine
wmachine23
zmachine23

# 7  
Old 04-01-2010
Hi All,

thanks for your quick and informative replies to my query.

In the end the following expressions met my requirements:

sed 's/\([[:alnum:]][[:alnum:]]*\)\.\([[:graph:]][[:graph:]]*\)/\1/g'

sed 's/ \([^ @.]*\)\.[^ @]* / \1 /g'

Thanks for the help.

K
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing quotes in sed

I have a file and I want to remove quotes from the word " ` " through sed command but unable to remove I am using below command sed s/"`XYZ`"/"ZXY"/g file1.txt > file2.txt But this is not working. How can we remove "`" through sed command (2 Replies)
Discussion started by: kaushik02018
2 Replies

2. Shell Programming and Scripting

Rsync script to rewrite suffix - BASH, awk, sed, perl?

trying to write up a script to put the suffix back. heres what I have but can't get it to do anything :( would like it to be name.date.suffix rsync -zrlpoDtub --suffix=".`date +%Y%m%d%k%M%S`.~" --bwlimit=1024 /mymounts/test1/ /mymounts/test2/ while IFS=. read -r -u 9 -d '' name... (1 Reply)
Discussion started by: jmituzas
1 Replies

3. Shell Programming and Scripting

sed removing values

Is there an easy way in sed to remove a character and values after that character? ex: blackout_10-11-2011(NODE_LEVEL) I want to remove '(' and everything after. so I will only have. blackout_10-11-2011 I can use a cut command to do this but interested in seeing how its done... (1 Reply)
Discussion started by: BeefStu
1 Replies

4. Shell Programming and Scripting

SED removing CR/LF

I have a source text file that contains something like this: <start_template> bool function( void *<var_name> ) { // Blah Blah } </start_template> Following is used to remove this template from source file. template=$(sed -n -e '/<start_template>/,/<\/start_template>/{... (4 Replies)
Discussion started by: gee22
4 Replies

5. Shell Programming and Scripting

SED to add a suffix

Hi all, Im trying to make a proper hosts.allow with the lists of sshbl.org to block the ssh brute force attackers. The list is a text file with an IP on every line. What I've gotten up sofar is to prefix "sshd : " on every line, but I need a " : deny" suffix behind every line as well. ... (9 Replies)
Discussion started by: necron
9 Replies

6. Shell Programming and Scripting

sed help - remove a period from a domain name

I have this code that parses the output of a reverse dns lookup to a domain name nslookup $IP | sed -n 's/.*arpa.*name = \(.*\)/\1/p' But my ouput is always randomsite.com. Notice the period tacked on to the end. How do I correct this so it is just randomsite.com Thank you. (1 Reply)
Discussion started by: streetfighter2
1 Replies

7. Windows & DOS: Issues & Discussions

How to: Linux BOX in Windows Domain (w/out joining the domain)

Dear Expert, i have linux box that is running in the windows domain, BUT did not being a member of the domain. as I am not the System Administrator so I have no control on the server in the network, such as modify dns entry , add the linux box in AD and domain record and so on that relevant. ... (2 Replies)
Discussion started by: regmaster
2 Replies

8. Shell Programming and Scripting

Removing a character using sed

Hi, I have a file with the text below. How do i remove the character "%" from the text file using sed ? Can anybody help ? 0% 68% 72% 0% 54% 33% 75% 24% 6% 59% 77% 77% 33% (6 Replies)
Discussion started by: Raynon
6 Replies

9. Shell Programming and Scripting

Removing lines with sed

Here is some code output that I have: architecture ppc cputype CPU_TYPE_POWERPC cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 4096 size 184464 align 2^12 (4096) architecture ppc64 cputype CPU_TYPE_POWERPC64 cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 192512 ... (5 Replies)
Discussion started by: pcwiz
5 Replies

10. Shell Programming and Scripting

Removing carriage returns with sed

How do we delete all carriage returns after a particular string using sed inside a K Shell? e.g. I have a text file named file1 below: $ more file1 Group#=1 User=A Role=a1 Group#=2 User=B Role=a1 Role=b1 Group#=3 User=C Role=b1 I want the carriage returns to be delete on the... (12 Replies)
Discussion started by: stevefox
12 Replies
Login or Register to Ask a Question