尝试从文本文件中获取所需的输出

尝试从文本文件中获取所需的输出

我在文本文件中有这样的输出,其中^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

相关内容