Print multiple variables in one line using python
Question: I need some assistance with a python script. I need to search a dhcpd file for
host entires, their MAC and IP, and print it in one line. I am able to locate
the hostname and IP address but cannot figure out how to get the variables out
of the if statement to put in one line. Any suggestions, the code is below:

    
    
    #!/usr/bin/python
    
    import sys
    import re
    
    #check for arguments
    
    if len(sys.argv) > 1:
        print "usage: no arguments required"
        sys.exit()
    else:
        dhcp_file = open("/etc/dhcp/dhcpd.conf","r")
        for line in dhcp_file:
            if re.search(r'\bhost\b',line):
                split = re.split(r'\s+', line)
                print split[1]
            if re.search(r'\bhardware ethernet\b',line):
                ip = re.split(r'\s+',line)
                print ip[2]
        dhcp_file.close()
    


Answer: There are a number of ways that you could go about this. The simplest is
probably to initialize an empty string before the if statements. Then, instead
of printing split[1] and ip[2], concatenate them to the empty string and print
that afterwards. So it would look something like this:

    
    
        printstr = ""
        if re.search...
            ...
            printstr += "Label for first item " + split[1] + ", "
        if re.search...
            ...
            printstr += "Label for second item " + ip[2]
        print printstr
    

