5

I am running AIX 5.3(not by choice, but I cannot change it), and I have a text file: servers.txt Here is an example of the file's contents:

apple port1 username password IPAddress TCP
banana port2 username password IPAddress TCP
beet port3 username password IPAddress TCP
apple port4 username password IPAddress TCP

avocado port1 username password IPAddress TCP
tomato port2 username password IPAddress TCP
avocado port3 username password IPAddress TCP
peach port4 username password IPAddress TCP
avocado port5 username password IPAddress TCP
avocado port6 username password IPAddress TCP

strawberry port1 username password IPAddress TCP
strawberry port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP

avocado port1 username password IPAddress TCP
lemon port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP
avocado port4 username password IPAddress TCP

I have a list file: newservers.lst, that has a list of four server names:

beet
banana
cherry
tomato

I need to walk through 'servers.txt', and sequentially replace all of the instances of server name "avocado" with names from 'newservers.lst'.

Here's the sort of thing I need to end up with:

apple port1 username password IPAddress TCP
banana port2 username password IPAddress TCP
beet port3 username password IPAddress TCP
apple port4 username password IPAddress TCP

beet port1 username password IPAddress TCP
tomato port2 username password IPAddress TCP
banana port3 username password IPAddress TCP
peach port4 username password IPAddress TCP
cherry port5 username password IPAddress TCP
tomato port6 username password IPAddress TCP

strawberry port1 username password IPAddress TCP
strawberry port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP

beet port1 username password IPAddress TCP
lemon port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP
banana port4 username password IPAddress TCP

Is there a way to accomplish this with a sed command, or will I need to use a do/while loop, or similar?

5

Try awk

awk '
    NR==FNR{
        A[NR]=$1
        limit=NR
        next
    }
    /^avocado/{
        i=i%limit+1
        $1=A[i]
    }
    {
        print
    }
    ' newservers.lst servers.txt

Sed is possible too:

sed '/^\s*\S\+\s*$/ {            #match 1st file only
        x                        #exchange line with holdspace
        H                        #add pre-holdspace to pre-line
        d                        #no print
    }                            #result: reversed 1st file in holdspace
    /^avocado/{
        G                        #add holdspace to line
        s/\S\+\(.*\)\n\(\w\+\)\n*$/\2\1/
                                 #replace 1st word by last word(from holdspace)
        P                        #print line before holdspace resedue
        s/\s[^\n]*//             #remove all from 1st word to holdspace
        h                        #return holdspace with last word became first
        d                        #no print
    }
    ' newservers.lst servers.txt
1

Python Solution:

#!/usr/bin/python3

#cycle.py

old_server = 'avocado'
new_servers = ['beet','banana','cherry','tomato']
replacement_count = 0 

with open('example.txt') as file:
    #cycle through file
    for line in file:
        if old_server in line:
            replacement_count += 1 #increment counter to cycle through new servers
            #print string with old server replaced with new
            print(line.strip().replace(old_server,new_servers[replacement_count%3],1))
        else:
            #print existing line if old server wasn't found
            print(line.strip())

With your input file as example.txt, this gives the output:

apple port1 username password IPAddress TCP
banana port2 username password IPAddress TCP
beet port3 username password IPAddress TCP
apple port4 username password IPAddress TCP

banana port1 username password IPAddress TCP
tomato port2 username password IPAddress TCP
cherry port3 username password IPAddress TCP
peach port4 username password IPAddress TCP
beet port5 username password IPAddress TCP
banana port6 username password IPAddress TCP

strawberry port1 username password IPAddress TCP
strawberry port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP

cherry port1 username password IPAddress TCP
lemon port2 username password IPAddress TCP
strawberry port3 username password IPAddress TCP
beet port4 username password IPAddress TCP
  • Pyhton is not something which is installed on a default AIX LPAR. But above can also be done in KSH / Perl etc – Mike van Hoof Feb 16 '16 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.