저는 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 '
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