Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format lines with different values in fields 1

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
A file contains a few thousand lines in the following format:

hostname1
Set: domainname

hostname2
Set: domainname

hostname3

hostname4

hostname5
Set: domainname

hostname6
-bash:

hostname7

hostname8
-bash

What I want is:
if the line below hostname is 'Set' then:
hostname: domainname

if the line below hostname is '-bash' then:
hostname: not found

if the line contains only hostname then:
hostname: no login

So from the above the output would be:
hostname1: domainname
hostname2: domainname
hostname3: no login
hostname4: no login
hostname5: domainname
hostname6: not found
hostname7: no login
hostname8: not found
 
Try this:

Code:
awk '
        !/^$/ {
                hostname=$1
                getline
                if (/^Set:/) print hostname ": " $2
                else if (/^-bash/) print hostname ": not found"
                else print hostname ": no login"
        }
' inputfile

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top