如何从目录树中的多个文件中search并删除一行(而不是replace)? 我已经尝试过诸如grepWin和Windows Grep这样的程序,但是它们replace了行,它们并没有删除整行。 什么是我最好的select?
例如:search“hello”并删除任何带有“hello”的行。
yo hello hey hey hi hello bye bye
应该回来
yo bye bye
假设这些文件的扩展名是.foo,那么下面的代码应该这样做:
for /f %f in ('dir /s /b *.foo') do findstr /v hello %f > c:\temp\x && move /yc:\temp\x %f
hth – 雷内
如果你有cygwin,你可以使用sed:
cat test.txt | sed 's/.*hello.*//'
这将删除与你好行,但显示空行。 如果你不喜欢黑线,你可以这样做:
cat test.txt | sed 's/.*hello.*//' | sed '/^$/d'
编辑:在第二个想法,完整的东西可以简化为:
sed '/.*hello.*/d' test.txt
其中test.txt是文件。
grep -R --revert-match
可能会工作。 一些GNU grep版本支持通过文件掩码进行递归搜索,但我不确定Windows端口。
更长但更简单的方法,
gfind . -name "*.txt" | xargs deleteLine.bat hello
gfind是从GNU中查找的,例如unxutils.sf.net和deleteLine.bat就像:
grep %1 --revert-match %2 > tempfile.### move tempfile.### %2
我建议安装cygwin如果你还没有它 – 它是充满了这样的事情的工具。
你可以用sed删除行:
sed -i '/pattern/d' filename...
-i选项告诉sed就地编辑文件。 -i.bak将会保留一个扩展名为.bak的备份文件。
要处理目录树中的所有文件,我建议在zsh中运行命令。 与bash相比,Zsh有一些时髦的文件名扩展选项,例如
sed -i '/pattern/d' **/*.(c|h)
将从当前目录下的每个文件运行sed,扩展名为.c或.h。
将Powershell定位在根目录中:
foreach($file in (dir -r -i *.txt -force)) { $cleaned = gc $file | where { $_ -notlike "*Hello*" }; sc $file $cleaned }
将* .txt过滤器和“ Hello ”匹配标准更改为任何你想要的。
perl -i.bak -ne'打印除非m / pattern /'file1 file2 file3 ...
我在grepWin或Windows Grep中找到了一个办法。
正则表达式搜索:^。 你好。 $ \ r \ n正则表达式替换:
(空白替换)