我试图根据时间范围删除文件的内容。到目前为止,我已经根据模式匹配进行了尝试,如下所示:
sed -i '/2018-03-09T13:53/,/2018-03-09T14:00/!d' LogAgentServer
文件(LogAgentServer)的内容如下:
2018-03-09T13:52:04.429+0000: 64.870: [GC (Allocation Failure) [PSYoungGen: 195634K->4192K(240640K)] 305630K->155712K(940032K), 0.1015550 secs] [Times: user=0.18 sys=0.02, real=0.10 secs]
2018-03-09T13:52:23.085+0000: 83.525: [GC (Allocation Failure) [PSYoungGen: 147552K->1600K(145408K)] 299072K->156324K(844800K), 0.0113755 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
2018-03-09T13:52:36.438+0000: 96.879: [GC (Allocation Failure) [PSYoungGen: 144960K->1376K(238080K)] 299684K->156488K(937472K), 0.0085403 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
2018-03-09T13:52:47.174+0000: 107.615: [GC (Allocation Failure) [PSYoungGen: 132448K->1344K(132608K)] 287560K->156544K(832000K), 0.0067113 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2018-03-09T13:52:58.583+0000: 119.023: [GC (Allocation Failure) [PSYoungGen: 132416K->1248K(240128K)] 287616K->156552K(939520K), 0.0072957 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
2018-03-09T13:53:13.075+0000: 133.516: [GC (Allocation Failure) [PSYoungGen: 131808K->1248K(239616K)] 287112K->156624K(939008K), 0.0142212 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:53:30.405+0000: 150.845: [GC (Allocation Failure) [PSYoungGen: 131808K->1792K(243712K)] 287184K->157248K(943104K), 0.0092942 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:53:43.380+0000: 163.820: [GC (Allocation Failure) [PSYoungGen: 137984K->4320K(241664K)] 293440K->160512K(941056K), 0.0141034 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:53:57.422+0000: 177.863: [GC (Allocation Failure) [PSYoungGen: 140512K->4512K(250880K)] 296704K->163920K(950272K), 0.0190071 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:55:07.187+0000: 247.627: [GC (Allocation Failure) [PSYoungGen: 153504K->3552K(247296K)] 312912K->166176K(946688K), 0.0113494 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:55:49.734+0000: 290.174: [GC (Allocation Failure) [PSYoungGen: 152544K->4256K(259072K)] 315168K->169024K(958464K), 0.0137216 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:56:24.438+0000: 324.878: [GC (Allocation Failure) [PSYoungGen: 169120K->4896K(254976K)] 333888K->172440K(954368K), 0.0231447 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:57:17.868+0000: 378.308: [GC (Allocation Failure) [PSYoungGen: 169760K->4880K(267264K)] 337304K->175953K(966656K), 0.0197401 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2018-03-09T13:58:38.692+0000: 459.133: [GC (Allocation Failure) [PSYoungGen: 186128K->5216K(263168K)] 357201K->179796K(962560K), 0.0123329 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:59:51.920+0000: 532.361: [GC (Allocation Failure) [PSYoungGen: 186464K->5312K(275968K)] 361044K->184116K(975360K), 0.0134409 secs] [Times: user=0.03 sys=0.01, real=0.02 secs]
2018-03-09T14:01:03.648+0000: 604.088: [GC (Allocation Failure) [PSYoungGen: 204352K->4832K(284160K)] 387421K->192292K(983552K), 0.0129953 secs] [Times: user=0.02 sys=0.01, real=0.01 secs]
2018-03-09T14:01:30.629+0000: 631.069: [GC (Allocation Failure) [PSYoungGen: 219872K->5088K(280064K)] 407332K->196181K(979456K), 0.0104791 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
但是这个命令的问题是,如果没有上述时间范围的条目,那么它将不起作用,那么我如何使用比较时间范围来实现这一点。
就像下面这样:
sed -i '/2018-03-09T13:54/,/2018-03-09T14:00/!d' LogAgentServer
它应该给出以下结果:
2018-03-09T13:55:07.187+0000: 247.627: [GC (Allocation Failure) [PSYoungGen: 153504K->3552K(247296K)] 312912K->166176K(946688K), 0.0113494 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:55:49.734+0000: 290.174: [GC (Allocation Failure) [PSYoungGen: 152544K->4256K(259072K)] 315168K->169024K(958464K), 0.0137216 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2018-03-09T13:56:24.438+0000: 324.878: [GC (Allocation Failure) [PSYoungGen: 169120K->4896K(254976K)] 333888K->172440K(954368K), 0.0231447 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:57:17.868+0000: 378.308: [GC (Allocation Failure) [PSYoungGen: 169760K->4880K(267264K)] 337304K->175953K(966656K), 0.0197401 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2018-03-09T13:58:38.692+0000: 459.133: [GC (Allocation Failure) [PSYoungGen: 186128K->5216K(263168K)] 357201K->179796K(962560K), 0.0123329 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2018-03-09T13:59:51.920+0000: 532.361: [GC (Allocation Failure) [PSYoungGen: 186464K->5312K(275968K)] 361044K->184116K(975360K), 0.0134409 secs] [Times: user=0.03 sys=0.01, real=0.02 secs]
答案1
尝试这个。
sed -e '/2018-03-09T13:5[4-9]/!d' LogAgentServer