从日志文件中删除换行符

时间:2019-02-04 21:46:29

标签: regex text-files

我只想删除“ ParNew”字符串后的换行符。

即ParNew的下一行应合并到ParNew行中。

2019-01-11T09:24:17.063+0800: 519253.299: [GC 519253.300: [ParNew
: 846970K->7882K(943744K), 0.0466880 secs] 74546294K->73707206K(75392640K), 0.0479230 secs] [Times: user=0.77 sys=0.00, real=0.05 secs] 
- age   3:        360 bytes,    6983240 total
- age   2:       1680 bytes,    6982880 total
- age   1:    6981200 bytes,    6981200 total
Desired survivor size 53673984 bytes, new threshold 4 (max 4)
2019-01-11T09:24:16.645+0800: 519252.881: [GC 519252.882: [ParNew
: 846986K->8058K(943744K), 0.0470990 secs] 74547818K->73708889K(75392640K), 0.0483330 secs] [Times: user=0.78 sys=0.00, real=0.05 secs] 
- age   4:        512 bytes,    7107392 total
- age   2:       1968 bytes,    7106880 total
- age   1:    7104912 bytes,    7104912 total

1 个答案:

答案 0 :(得分:1)

Perl允许多行正则表达式。尝试以下行:

perl -ne 's/ParNew\n/ParNew/g; print;' your_file.txt

结果:

2019-01-11T09:24:17.063+0800: 519253.299: [GC 519253.300: [ParNew: 846970K->7882K(943744K), 0.0466880 secs] 74546294K->73707206K(75392640K), 0.0479230 secs] [Times: user=0.77 sys=0.00, real=0.05 secs] 
- age   3:        360 bytes,    6983240 total
- age   2:       1680 bytes,    6982880 total
- age   1:    6981200 bytes,    6981200 total
Desired survivor size 53673984 bytes, new threshold 4 (max 4)
2019-01-11T09:24:16.645+0800: 519252.881: [GC 519252.882: [ParNew: 846986K->8058K(943744K), 0.0470990 secs] 74547818K->73708889K(75392640K), 0.0483330 secs] [Times: user=0.78 sys=0.00, real=0.05 secs] 
- age   4:        512 bytes,    7107392 total
- age   2:       1968 bytes,    7106880 total
- age   1:    7104912 bytes,    7104912 total

这里:

  • -e允许执行命令(模式替换)
  • -n将命令嵌入到一个隐式循环中,该循环将遍历从stdin获得的每一行
相关问题