【发布时间】:2016-12-13 16:44:34
【问题描述】:
我有一个txt文件,格式如下:
目的是删除以“Subtotal Group 1”或“Subtotal Group 2”或“Grand Total”开头的行(此类字符串始终位于行首),但我需要删除它们仅当该行的其余部分有空白字段(或用空格填充)时。
这可以通过 awk 或 sed(1 次通过)来实现,但我目前正在使用 3 个单独的步骤(每个文本一个)。更通用的语法会很棒。谢谢大家。
我的 txt 文件如下所示:
Some Generic Headers at the beginning of the file
=======================================================================
Group 1
=======================================================================
6.00 500 First Line Text 1685.52
1.00 502 Second Line Text 280.98
530 Other Line text 157.32
_________________________________________________________________________
Subtotal Group 1
Subtotal Group 1
Subtotal Group 1
Subtotal Group 1 2123.82
Subtotal Group 1
Subtotal Group 1
========================================================================
GROUP 2
========================================================================
7.00 701 First Line Text 53.63
711 Second Line text 97.85
7.00 740 Third Line text 157.32
741 Any Line text 157.32
742 Any Line text 18.04
801 Last Line text 128.63
_______________________________________________________________________
Subtotal Group 2
Subtotal Group 2
Subtotal Group 2
Subtotal Group 2
Subtotal Group 2 612.79
Subtotal Group 2
_______________________________________________________________________
Grand total
Grand total
Grand total
Grand total
Grand total
Grand total
Grand total 1511.03
我想要达到的目标输出是:
Some Generic Headers at the beginning of the file
=======================================================================
Group 1
=======================================================================
6.00 500 First Line Text 1685.52
1.00 502 Second Line Text 280.98
530 Other Line text 157.32
_______________________________________________________________________
Subtotal Group 1 2123.82
=======================================================================
GROUP 2
=======================================================================
7.00 701 First Line Text 53.63
711 Second Line text 97.85
7.00 740 Third Line text 157.32
741 Any Line text 157.32
742 Any Line text 18.04
801 Last Line text 128.63
_______________________________________________________________________
Subtotal Group 2 612.79
_______________________________________________________________________
Grand total 1511.03
【问题讨论】:
-
什么是
Field1,...数字?它们是否以Subtotal或Grand Total以外的任何其他开头? -
@David 你说得对,这很令人困惑,我要编辑这个问题。谢谢。
-
@EdMorton 我有一个 CSV(在前几天你帮了我很多忙来格式化它并转换成可读的格式化 txt,并对齐)。现在我实现了一个几乎可打印的 txt,最后要修复的是删除多余的无用行。可能以前可以使用更有效的编码,但是我不太擅长在单个脚本中找出所有步骤,所以我正在逐步进行。谢谢埃德!
-
PD:如果我必须删除帖子或重新编写它以避免“超出主题”,我可以做到。
-
@EdMorton 我完全同意,我只是重新表述,抱歉让 Ed 感到困惑!