Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip last period from a host string

I am having a hard time figuring out how to strip the last period from a hostname ...

current output:

  • domain.com.
  • suddomain.com.
  • domain.com.
  • subdomain.subdomain.com.
  • subdomain.com.

desired output:

  • domain.com
  • subdomain.com
  • domain.com
  • subdomain.subdomain.com

attempt 1:

print string[:-1]  #it works on some lines but not all

attempt 2:

 str = string.split('.')
 subd = '.'.join(str[0:-1])
 print subd    # does not work at all 

code:

global DOMAINS

if len(DOMAINS) >= 1:
  for domain in DOMAINS:
    cmd = "dig @adonis.dc1.domain.com axfr %s |grep NS |awk '{print $1}'|sort -u |grep -v '^%s.$'" % (domain,domain)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,   shell=True)
    string = p.stdout.read()
    string = string.strip().replace(' ','')
    if string:
      print string
like image 367
Simply Seth Avatar asked Feb 18 '13 23:02

Simply Seth


People also ask

What is the use of strip () method for string?

The string strip() method in python is built-in from Python. It helps the developer to remove the whitespaces or specific characters from the string at the beginning and end of the string. Strip() method in string accepts only one parameter which is optional and has characters.

How do you remove the last period of a string in Python?

Use Python to Remove Punctuation from a String with Translate. One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate method typically takes a translation table, which we'll do using the .


1 Answers

You do it like this:

hostname.rstrip('.')

where hostname is the string containing the domain name.

>>> 'domain.com'.rstrip('.')
'domain.com'
>>> 'domain.com.'.rstrip('.')
'domain.com'
like image 84
isedev Avatar answered Oct 21 '22 10:10

isedev