从文件中删除给定模式之间的行(使用Unix工具) [英] Remove lines which are between given patterns from a file (using Unix tools)
问题描述
我有一个文本文件(更正确地说是德国风格" CSV文件,即用分号分隔的十进制逗号),该文件在每行上都有一个日期和一个测量值.
在继续工作之前,我想消除一些错误的值.我想将这些剪切存储在一些脚本中,以便记录我的更正,并在必要时可以重播这些更正.
I have a text file (more correctly, a “German style“ CSV file, i.e. semicolon-separated, decimal comma) which has a date and the value of a measurement on each line.
There are stretches of faulty values which I want to remove before further work. I'd like to store these cuts in some script so that my corrections are documented and I can replay those corrections if necessary.
这些行看起来像这样:
28.01.2005 14:48:38;5,166
28.01.2005 14:50:38;2,916
28.01.2005 14:52:38;0,000
28.01.2005 14:54:38;0,000
(long stretch of values that should be removed; could also be something else beside 0)
01.02.2005 00:11:43;0,000
01.02.2005 00:13:43;1,333
01.02.2005 00:15:43;3,250
现在,我想存储诸如28.01.2005 14:52:38
+ 01.02.2005 00:11:43
之类的开始和结束模式的列表,脚本将剪切匹配这些开始/结束对以及它们之间的所有内容的行.
Now I'd like to store a list of begin and end patterns like 28.01.2005 14:52:38
+ 01.02.2005 00:11:43
, and the script would cut the lines matching these begin/end pairs and everything that's between them.
我正在考虑黑客awk脚本,但也许我想念一个已经存在的工具.
I'm thinking about hacking an awk script, but perhaps I'm missing an already existing tool.
推荐答案
看看sed
:
sed '/start_pat/,/end_pat/d'
将删除start_pat
和end_pat
(包括)之间的行.
will delete lines between start_pat
and end_pat
(inclusive).
要删除多个这样的对,可以将它们与多个-e
选项组合:
To delete multiple such pairs, you can combine them with multiple -e
options:
sed -e '/s1/,/e1/d' -e '/s2/,/e2/d' -e '/s3/,/e3/d' ...
这篇关于从文件中删除给定模式之间的行(使用Unix工具)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!