Como encontrar uma palavra e substituí-la sequencialmente?

5

Estou executando o AIX 5.3 (não por opção, mas não posso alterá-lo) e tenho um arquivo de texto: servers.txt Aqui está um exemplo do conteúdo do arquivo:

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

Eu tenho um arquivo de lista: newservers.lst, que tem uma lista de quatro nomes de servidor:

beet
banana
cherry
tomato

Eu preciso percorrer 'servers.txt' e, sequencialmente, substituir todas as instâncias do nome do servidor "abacate" pelos nomes de 'newservers.lst'.

Aqui está o tipo de coisa que eu preciso terminar:

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

Existe uma maneira de realizar isso com um comando sed ou eu precisarei usar um loop do / while ou similar?

    
por Chris Boone 01.02.2016 / 18:22

2 respostas

5

Experimente o awk

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

Sed também é possível:

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*$//
                                 #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
    
por 01.02.2016 / 19:55
1

Solução Python:

#!/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())

Com seu arquivo de entrada como example.txt , isso fornece a saída:

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
    
por 02.02.2016 / 20:07