
私は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
答え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