^M
CR이 나타내는 텍스트 파일에 이 출력이 있습니다.
Last login: Sat Jan 22 09:49:50 2023 from 10.1.1.1^M
Cisco Router
uptime
exit -f
Connecting user mike from 10.1.1.1
****************************************** Warning ******************************************^M
^M
Warning monitoring.^M
^M
*********************************************************************************************^M
[ ne ]^M
[email protected]> uptime^M
^[[1A^[[2K^[[1A^[[[email protected]> uptime^M
9 days, 3:43^M
[ ne ]^M
[email protected]> exit -f^M
Shutting down the session^M
^M
Connection closed
Last login: Sat Jan 21 09:49:49 2023 from 10.1.1.1^M
Cisco Router
uptime
exit -f
Connecting user mike from 10.1.1.1
****************************************** Warning ******************************************^M
^M
Warning monitoring.^M^M
^M
*********************************************************************************************^M
^M
[ ne ]^M
[email protected]> uptime^M
^[[1A^[[2K^[[1A^[[[email protected]> uptime^M
10 days, 3:43^M
[ ne ]^M
[email protected]> exit -f^M
Shutting down the session^M
^M
Connection closed
Last login: Sat Jan 21 09:49:49 2023 from 10.1.1.1^M
Cisco Router
uptime
exit -f
Connecting user mike from 10.1.1.1
****************************************** Warning ******************************************^M
^M
^M
*********************************************************************************************^M
^M
[ ne ]^M
[email protected]> uptime^M
^[[1A^[[2K^[[1A^[[[email protected]> uptime^M
11 days, 3:43^M
[ ne ]^M
[email protected]> exit -f^M
Shutting down the session^M
^M
Connection closed
Last login: Sat Jan 21 09:49:49 2023 from 10.1.1.1^M
Cisco Router
uptime
exit -f
Connecting user mike from 10.1.1.1
****************************************** Warning ******************************************^M
^M
^M
*********************************************************************************************^M
^M
[ ne ]^M
[email protected]> uptime^M
^[[1A^[[2K^[[1A^[[[email protected]> uptime^M
12 days, 3:43^M
[ ne ]^M
[email protected]> exit -f^M
Shutting down the session^M
^M
Connection closed
^M
나는 이런 결과를 얻으려고 노력하고 있습니다.
grv1.nue99.de> uptime 9 days, 3:43
grv2.nue99.de> uptime 10 days, 3:43
grv1.nue98.de> uptime 11 days, 3:43
grv2.nue98.de> uptime 12 days, 3:43
나는 열심히 노력했다
awk '/mike@.*> uptime/ {print;getline;print}' testbeta1.text > testbeta2.txt
sed 's/mike@//g; s/> uptime/> uptime /g' test.txt > test1.txt
grep -oP 'mike@.*uptime.*\K[0-9]+\sdays.*' testbeta.text
누군가 내가 원하는 결과를 얻을 수 있도록 도와줄 수 있나요?
답변1
또 다른 awk
솔루션. 나는 tr
모든 줄에서 CR 문자를 제거 하곤 했습니다 . 전체 내용(주석 제외 #
)은 아마도 상대적으로 읽기 어려운 한 줄로 압축될 수 있지만 여기에서는 가독성을 돕기 위해 여러 줄에 걸쳐 표시했습니다.
tr -d '\r' <script |
awk -F'[@ ]' '
# If we have a hostname set, print it and the uptime value; reset for next time
host && /:/ {printf "%s uptime %s\n", host, $0; host=""}
# Capture hostname if input matches ">" from the prompt and keyword "uptime"
!host && />/ && /uptime/ {host=$2}
'
예제 출력
grv1.nue99.de> uptime 9 days, 3:43
grv2.nue99.de> uptime 10 days, 3:43
grv1.nue98.de> uptime 11 days, 3:43
grv2.nue98.de> uptime 12 days, 3:43
답변2
이 시도:
cat testbeta1.text | tr -d "\r" | tr "\n" " " | tr -s " " | grep -Eo 'mike@[^>]+> uptime [0-9]+ days, [0-9]+:[0-9]+' | cut -d "@" -f 2-
설명하다:
tr -d "\r"
:모든 CR 문자를 제거합니다.tr "\n" " "
: 각 LF 문자를 공백으로 바꿉니다.tr -s " "
:반복되는 공백의 각 순서를 하나의 공백으로 바꿉니다.grep -Eo 'mike@[^>]+> uptime [0-9]+ days, [0-9]+:[0-9]+'
-E
: 정규식을 허용하는 모든 시퀀스를 가져오고 ( ) 해당 시퀀스만 가져옵니다(-o
)cut -d "@" -f 2-
@
: 줄의 첫 번째 ' ' 문자 다음 부분을 가져옵니다.
당신의 예를 들어보세요:
> cat testbeta1.text | tr -d "\r" | tr "\n" " " | tr -s " " | grep -Eo 'mike@[^>]+> uptime [0-9]+ days, [0-9]+:[0-9]+' | cut -d "@" -f 2-
grv1.nue99.de> uptime 9 days, 3:43
grv2.nue99.de> uptime 10 days, 3:43
grv1.nue98.de> uptime 11 days, 3:43
grv2.nue98.de> uptime 12 days, 3:43