如何从文件中删除垃圾字符?

时间:2014-09-29 14:34:44

标签: regex perl

我有一个包含以下文字的文件。如何删除"垃圾"像^[[H这样的字符使用Perl?

^[[H^[[2J^[(B^[[mtop - 19:25:22 up 69 days, 23:25,  2 users,  load average: 2.55, 2.15, 1.83^[(B^[[m^[[39;49m^[[K
Tasks:^[(B^[[m^[[39;49m^[(B^[[m 114 ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m   1 ^[(B^[[m^[[39;49mrunning,^[(B^[[m^[[39;49m^[(B^[[m 113 ^[(B^[[m^[[39;49msleeping,^[(B^[[m^[[39;49m^[(B^[[m   0 ^[(B^[[m^[[39;49mstopped,^[(B^[[m^[[39;49m^[(B^[[m   0 ^[(B^[[m^[[39;49mzombie^[(B^[[m^[[39;49m^[[K
Cpu(s):^[(B^[[m^[[39;49m^[(B^[[m 18.1%^[(B^[[m^[[39;49mus,^[(B^[[m^[[39;49m^[(B^[[m  0.5%^[(B^[[m^[[39;49msy,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mni,^[(B^[[m^[[39;49m^[(B^[[m 81.2%^[(B^[[m^[[39;49mid,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mwa,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mhi,^[(B^[[m^[[39;49m^[(B^[[m  0.2%^[(B^[[m^[[39;49msi,^[(B^[[m^[[39;49m^[(B^[[m  0.0%^[(B^[[m^[[39;49mst^[(B^[[m^[[39;49m^[[K
em: ^[(B^[[m^[[39;49m^[(B^[[m 16435100k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m  3081324k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m 13353776k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m   196396k ^[(B^[[m^[[39;49mbuffers^[(B^[[m^[[39;49m^[[K
Swap:^[(B^[[m^[[39;49m^[(B^[[m  4194296k ^[(B^[[m^[[39;49mtotal,^[(B^[[m^[[39;49m^[(B^[[m        0k ^[(B^[[m^[[39;49mused,^[(B^[[m^[[39;49m^[(B^[[m  4194296k ^[(B^[[m^[[39;49mfree,^[(B^[[m^[[39;49m^[(B^[[m  1531300k ^[(B^[[m^[[39;49mcached^[(B^[[m^[[39;49m^[[K
^[[6;1H
^[[7m  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                    ^[(B^[[m^[[39;49m^[[K
^[(B^[[m22285 root      25   0 4931m 398m  11m S 47.6  2.5 545:13.49 java                                                                                                       ^[(B^[[m^[[39;49m
^[(B^[[m19235 root      17   0 1406m 624m  10m S  2.0  3.9   6203:15 java                                                                                                       ^[(B^[[m^[[39;49m
^[(B^[[m    1 root      15   0 10368  684  572 S  0.0  0.0   0:09.51 init                                                                                                       ^[(B^[[m^[[39;49m
^[(B^[[m    2 root      RT  -5     0    0    0 S  0.0  0.0   2:02.87 migration/0                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    3 root      34  19     0    0    0 S  0.0  0.0   0:00.27 ksoftirqd/0                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    4 root      RT  -5     0    0    0 S  0.0  0.0   2:00.50 migration/1                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    5 root      34  19     0    0    0 S  0.0  0.0   0:00.26 ksoftirqd/1                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    6 root      RT  -5     0    0    0 S  0.0  0.0   2:04.21 migration/2                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    7 root      34  19     0    0    0 S  0.0  0.0   0:00.26 ksoftirqd/2                                                                                                ^[(B^[[m^[[39;49m
^[(B^[[m    8 root      RT  -5     0    0    0 S  0.0  0.0   1:52.52 migration/3                                                                                                ^[(B^[[m^[[39;49m

1 个答案:

答案 0 :(得分:3)

您可以使用Term::ANSIColor模块,

perl -MTerm::ANSIColor=colorstrip -ne 'print colorstrip $_' file