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

4つのサーバー名のリストを含む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 '
    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

おすすめ記事