我正在尝试清理项目的一些文件并需要一些帮助。我的目标是遍历文件并删除模式后的所有内容
我当前的文件如下所示
nfr01wrp01:8440:
nfr01wrp01:8441: Server uptime: 19 days 23 hours 58 minutes 5 seconds
Total accesses: 559 - Total Traffic: 15.5 MB
CPU Usage: u124.03 s51.63 cu0 cs0 - .0102% CPU load
.000324 requests/sec - 9 B/second - 28.4 kB/request
nfr01wrp02:8442: Server uptime: 21 days 21 hours 28 minutes 54 seconds
Total accesses: 41 - Total Traffic: 593 kB
CPU Usage: u126.47 s52.63 cu0 cs0 - .00947% CPU load
2.17e-5 requests/sec - 0 B/second - 14.5 kB/request
ngr01wrp01:8443:
kfr01wrp02:8444:
ngr01wrp02:8445: Server uptime: 21 days 21 hours 28 minutes 56 seconds
Total accesses: 43 - Total Traffic: 664 kB
CPU Usage: u144.71 s62.25 cu0 cs0 - .0109% CPU load
2.27e-5 requests/sec - 0 B/second - 15.4 kB/request
nfr01wrp02:8446:
我想要的输出是
nfr01wrp01:8441: Server uptime: 19 days 23 hours 58 minutes 5 seconds
Total accesses: 559 - Total Traffic: 15.5 MB
CPU Usage: u124.03 s51.63 cu0 cs0 - .0102% CPU load
.000324 requests/sec - 9 B/second - 28.4 kB/request
nfr01wrp02:8442: Server uptime: 21 days 21 hours 28 minutes 54 seconds
Total accesses: 41 - Total Traffic: 593 kB
CPU Usage: u126.47 s52.63 cu0 cs0 - .00947% CPU load
2.17e-5 requests/sec - 0 B/second - 14.5 kB/request
ngr01wrp02:8445: Server uptime: 21 days 21 hours 28 minutes 56 seconds
Total accesses: 43 - Total Traffic: 664 kB
CPU Usage: u144.71 s62.25 cu0 cs0 - .0109% CPU load
2.27e-5 requests/sec - 0 B/second - 15.4 kB/request
所以我需要删除第二个 : 之后没有任何内容的任何行。
我尝试过以下方法
grep -o '*wrp0*:*:' clean.tmp > clean.out - this deleted everything
grep -v '^$' clean.tmp > clean.out - this deleted blank lines only
任何帮助将不胜感激。
答案1
$ grep -vE ':.+:$' input.txt
nfr01wrp01:8441: Server uptime: 19 days 23 hours 58 minutes 5 seconds
Total accesses: 559 - Total Traffic: 15.5 MB
CPU Usage: u124.03 s51.63 cu0 cs0 - .0102% CPU load
.000324 requests/sec - 9 B/second - 28.4 kB/request
nfr01wrp02:8442: Server uptime: 21 days 21 hours 28 minutes 54 seconds
Total accesses: 41 - Total Traffic: 593 kB
CPU Usage: u126.47 s52.63 cu0 cs0 - .00947% CPU load
2.17e-5 requests/sec - 0 B/second - 14.5 kB/request
ngr01wrp02:8445: Server uptime: 21 days 21 hours 28 minutes 56 seconds
Total accesses: 43 - Total Traffic: 664 kB
CPU Usage: u144.71 s62.25 cu0 cs0 - .0109% CPU load
2.27e-5 requests/sec - 0 B/second - 15.4 kB/request
这将输出所有行不匹配模式:.+:$
(一个冒号、一个或多个任意字符、另一个冒号,然后是行尾标记'。输出确切地匹配您的示例所需的输出(使用 和 进行diff
测试cmp
)
在正则表达式中使用+
表示一个或多个需要扩展正则表达式 (ERE) 语法,因此-E
使用 grep 的选项。注意一些(例如 GNU grep),但并非所有版本的 grep 都允许+
转义以\
具有该含义 -grep -v ':.\+:$'
在这种情况下也可以工作。