Python解决方案:

Python解决方案:

我正在运行 AIX 5.3(不是选择的,但我无法更改它),并且我有一个文本文件:servers.txt 以下是该文件内容的示例:

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

我有一个列表文件:newservers.lst,其中包含四个服务器名称的列表:

beet
banana
cherry
tomato

我需要遍历“servers.txt”,并按顺序将服务器名称“avocado”的所有实例替换为“newservers.lst”中的名称。

这是我需要完成的事情:

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

有没有办法使用 sed 命令来完成此操作,或者我需要使用 do/while 循环或类似命令?

答案1

尝试awk

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

塞德也是可能的:

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

答案2

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())

当您的输入文件为 时example.txt,将给出输出:

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

相关内容