用于检测和删除文件中文本的Windows命令
我有一个ascii文件,其中有一行: 开始 稍后在网上: 结束 我希望能够从windows中的命令行调用中删除这两行以及这两行之间的所有内容。这需要完全自动化 编辑:有关如何使用sed执行此操作的详细信息,请参阅(cygwin已使用sed) 编辑:我发现SED可以工作,但当我将输出管道传输到文件时,回车符已经被删除。我怎么能保存这些?使用此sed正则表达式: /^GlobalSection(TeamFoundationVersionControl)=预解$/,/^EndGlobalSection$/{ /^GlobalSection(TeamFoundationVersionControl)=预解决$/{ /^EndGlobalSection$/!d } } 。。其中开始部分为“GlobalSection(TeamFoundationVersionControl)=预解”,结束部分为“EndGlobalSection”。我还想删除这些行 编辑:我现在对sed使用更简单的方法: /^GlobalSection(TeamFoundationVersionControl)=预解决$/,/^EndGlobalSection$/d用于检测和删除文件中文本的Windows命令,windows,command-line,text,sed,Windows,Command Line,Text,Sed,我有一个ascii文件,其中有一行: 开始 稍后在网上: 结束 我希望能够从windows中的命令行调用中删除这两行以及这两行之间的所有内容。这需要完全自动化 编辑:有关如何使用sed执行此操作的详细信息,请参阅(cygwin已使用sed) 编辑:我发现SED可以工作,但当我将输出管道传输到文件时,回车符已经被删除。我怎么能保存这些?使用此sed正则表达式: /^GlobalSection(TeamFoundationVersionControl)=预解$/,/^EndGlobalSection
换行代码仍然是一个问题,尽管或者,我现在使用的是一种脚本语言,可以很好地与Ruby或Python等windows一起执行此类任务。Ruby很容易在windows中安装,并且会产生像这个孩子的游戏这样的问题 下面是一个可以使用的脚本,如: cutbegined.rb myFileName.txt
sourcefile = File.open(ARGV[0])
# Get the string and do a multiline replace
fileString = sourceFile.read()
slicedString = fileString.gsub(/BEGIN.*END\n/m,"")
#Overwrite the file
sourcefile.pos = 0
sourcefile.print slicedString
sourcefile.truncate(f.pos)
这做得很好,允许很大的灵活性,并且可能比sed更可读。这里有一个1行Perl命令,它可以执行您想要的操作(只需在命令提示符窗口中键入它): 回车和换行将得到妥善保存。原始版本的
myfile.txt
将另存为myfile.txt.bak
如果未安装Perl,请获取。以下是如何使用C#正则表达式删除整个GlobalSection(TeamFoundationVersionControl)=预解决部分:
// Create a regex to match against an entire GlobalSection(TeamFoundationVersionControl) section so that it can be removed (including preceding and trailing whitespace).
// The symbols *, +, and ? are greedy by default and will match everything until the LAST occurrence of EndGlobalSection, so we must use their non-greedy counterparts, *?, +?, and ??.
// Example of string to match against: " GlobalSection(TeamFoundationVersionControl) ...... EndGlobalSection "
Regex _regex = new Regex(@"(?i:\s*?GlobalSection\(TeamFoundationVersionControl\)(?:.|\n)*?EndGlobalSection\s*?)", RegexOptions.Compiled);
如果你对上一个问题的回答不满意,那么就说出来。这个新问题是等价的。关于hte线路馈电问题还有其他想法吗?这可能是因为新文件的管道:>newfile我需要它来管道每一行,包括新行字符
// Create a regex to match against an entire GlobalSection(TeamFoundationVersionControl) section so that it can be removed (including preceding and trailing whitespace).
// The symbols *, +, and ? are greedy by default and will match everything until the LAST occurrence of EndGlobalSection, so we must use their non-greedy counterparts, *?, +?, and ??.
// Example of string to match against: " GlobalSection(TeamFoundationVersionControl) ...... EndGlobalSection "
Regex _regex = new Regex(@"(?i:\s*?GlobalSection\(TeamFoundationVersionControl\)(?:.|\n)*?EndGlobalSection\s*?)", RegexOptions.Compiled);