Linux 시스템에서 명령을 실행하고 출력을 파일로 리디렉션하고 있습니다. 내 파일은 다음과 같습니다.
client: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-tuvwxyza
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
connection-1: frdp::Green::Test
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-abcedfgh
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-ijklmnop
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
client: tc-abcdefg-higj-klmn-opqr-qrstuvwx
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-qrstuvwx
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-qrstuvwx
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-12345678
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-12345678
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-12345678
connection-0: frdp::Green::Test
client: tc-abcdefg-higj-klmn-opqr-09876543
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-09876543
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-09876543
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-135792468
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-135792468
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-135792468
connection-0: frdp::Green::Test
client: tc-abcdefgh-igjk-lmno-pqrs-08649753
version: xxxx
uuid: abcdefgh-igjk-lmno-pqrs-08649753
user-uid: 10000
user: someuser
primary-server: xxx.xxx.xxx.xxx
operational-mode: normal
mac: XX:XX:XX:XX:XX:XX
locked-time: xx:xx:xx
ipaddr: xxx.xxx.xxx.xxx
hostname: tc-abcdefgh-igjk-lmno-pqrs-08649753
connection-0: frdp::Purple::Test
Purple에 대한 연결만 표시하는 클라이언트 호스트 이름과 연결이 없는 클라이언트를 새 파일로 추출하고 싶습니다. 다음을 사용하여 관련 없는 정보를 모두 성공적으로 얻었습니다.
awk -F '\t' '/^client: / || /connection/' myfile1 >> myfile2
보라색으로 연결되어 있거나 연결이 없는 클라이언트만 추출할 수는 없습니다. 클라이언트가 보라색에 연결되어 있더라도 녹색에 연결된 모든 것을 무시하고 싶습니다.
편집: 이것은 awk를 사용할 때 얻는 결과입니다.
client: tc-abcdefgh-igjk-lmno-pqrs-tuvwxyza
connection-1: frdp::Green::Test
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
client: tc-abcdefg-higj-klmn-opqr-qrstuvwx
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-12345678
connection-0: frdp::Green::Test
client: tc-abcdefg-higj-klmn-opqr-09876543
connection-0: frdp::Purple::Test
client: tc-abcdefgh-igjk-lmno-pqrs-135792468
connection-0: frdp::Green::Test
client: tc-abcdefgh-igjk-lmno-pqrs-08649753
connection-0: frdp::Purple::Test
편집: 나는 내 모호함을 고려하지 않았습니다. 내가 찾고 있는 출력은 다음과 같아야 합니다.
tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
tc-abcdefg-higj-klmn-opqr-qrstuvwx
tc-abcdefg-higj-klmn-opqr-09876543
tc-abcdefgh-igjk-lmno-pqrs-08649753
저는 Purple에만 연결되어 있지만 연결되지 않은 5개의 클라이언트를 원합니다.
답변1
모든 Unix 시스템의 모든 쉘에서 awk를 사용하십시오.
$ cat tst.awk
$1 == "client:" {
prt()
}
/connection/ {
gotConns = 1
if ( /Purple/ ) {
gotTarget = 1
}
else {
gotOther = 1
}
}
$1 == "hostname:" {
host = $2 ORS
}
END {
prt()
}
function prt( found) {
if ( (gotTarget && !gotOther) || !gotConns ) {
printf "%s", host
}
host = gotConns = gotTarget = gotOther = ""
}
$ awk -f tst.awk file
tc-abcdefgh-igjk-lmno-pqrs-abcdefgh
tc-abcdefgh-igjk-lmno-pqrs-ijklmnop
tc-abcdefgh-igjk-lmno-pqrs-qrstuvwx
tc-abcdefgh-igjk-lmno-pqrs-09876543
tc-abcdefgh-igjk-lmno-pqrs-08649753
답변2
내가 올바르게 이해했다면:
$ tr -s '\n' ' ' < file | \
sed 's/client:/\n/g' | \
grep -o 'hostname: [^ ]\+ connection-0.*Purple\|hostname: [^ ]\+ $' | \
cut -d' ' -f2
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
$ tr -s '\n' ' '
파일을 단일 문자열로 변환합니다.sed 's/client:/\n/g'
"클라이언트"를 개행 문자로 바꾸십시오.grep -o 'hostname: [^ ]\+ connection-0.*Purple\|hostname: [^ ]\+ $'
"connection-0...Purple"이 있는 라인이나 연결이 없는 라인을 캡처합니다.hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple hostname: tc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection-0: frdp::Purple
cut -d' ' -f2
호스트 이름만 인쇄합니다.
새 파일을 쓰려면 > newfile
.