Vista中的sed - 如何删除之间的所有符号? [英] sed in Vista - how to delete all symbols between?
问题描述
我有一个 bat 文件,我应该用它来删除一个文件的一部分并保存到另一个文件中.我需要删除文本[aaa bbb]"和[ccc ddd]"之间的所有符号.那就是如果我有文字:
I have a bat file that I should use to delete a part of one file and save into another one. I need to delete all the symbols between text "[aaa bbb]" and "[ccc ddd]". That is if I have the text:
[aaa bbb]
1
2
3
[ccc ddd]
我应该有作为输出:
[aaa bbb]
[ccc ddd]
谢谢
我想澄清这个问题.我应该删除marker1 和marker2 之间的所有符号.Marker1 和 Marker2 只是一些单词或文本的一部分,而不是强制行.例如我会:
I would like to clarify the question. I should delete all the symbols between marker1 and marker2. Marker1 and marker2 are just some words or parts of text but not obligatory lines. For example I would have:
[aaa bbb] [ccc]
1
2
3
4
5
[www yyy]
如果我想删除 [aaa bbb] 和 [www yyy] 之间的文本,我应该将其作为输出:
If I want to delete the text between [aaa bbb] and [www yyy] I should have as output:
[aaa bbb]
[www yyy]
推荐答案
查看 这个 sed 提示页面
将其应用于您的示例.clean.sed:
Applying it on your example. clean.sed:
/^\[aaa bbb\]$/,/^\[ccc ddd\]$/{
/^\[aaa bbb\]$/!{
/^\[ccc ddd\]$/!d
}
}
运行使用:
sed -f clean.sed inputfile.txt
要就地"编辑输入文件,请使用 sed 的 -i 选项:
To edit the input file "in place", use the -i option to sed:
sed -i.bak -f clean.sed datafile.txt
在编辑原始文件之前保存名为datafile.txt.bak"的文件的备份副本.
A backup copy of the file with the name "datafile.txt.bak" is saved before editing the original.
由于假设标记总是在自己的一行上是错误的,这里有一个可以处理一行中间标记的脚本:
Since the assumption that the markers where always on a line of their own was wrong, heres a script that can handle markers in the middle of a line:
/\[aaa bbb\]/,/\[ccc ddd\]/{
s/\[aaa bbb\].*/[aaa bbb]/
s/.*\[ccc ddd\]/[ccc ddd]/
/\[aaa bbb\]$/!{
/^\[ccc ddd\]/!d
}
}
对于这个输入:
foo[aaa bbb]1
2
3
4
5[ccc ddd]bar
foo
[aaa bbb]
1
2
3
[ccc ddd]
bar
它产生:
foo[aaa bbb]
[ccc ddd]bar
foo
[aaa bbb]
[ccc ddd]
bar
注意!它不能处理标记可以出现在同一行上的文件.
Note! It can't handle files where the markers can appear on the same line.
再次如果标记 1 的输入格式是这样的,您总是可以指望它在自己的一行上,您可以将脚本简化一些:
EDIT again: If the input format for marker 1 is such that you can always count on it being on a line of its own you can simplify the script some:
/^\[aaa bbb\]$/,/\[ccc ddd\]/{
s/.*\[ccc ddd\]/[ccc ddd]/
/^\[aaa bbb\]$/!{
/^\[ccc ddd\]/!d
}
}
(在行首和行尾锚定标记 1 并跳过标记 1 行的修剪.)
(Anchoring marker 1 at the beginning and end of a line and skipping the trimming of the marker 1 line.)
这篇关于Vista中的sed - 如何删除之间的所有符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!