[英]Regex grep file contents and invoke command
我有一个生成的文件包含MD5信息和文件名。 我想要从他们所在的目录中删除文件。我不确定如何准确地执行此操作。
文件列表(文件)包含:
MD5 (dupe) = 1fb218dfef4c39b4c8fe740f882f351a
MD5 (somefile) = a5c6df9fad5dc4299f6e34e641396d38
我的命令(我希望包含rm
)看起来像这样:
grep -o "\((.*)\)" filelist
返回:
(dupe)
(somefile)
*差不多好,虽然括号需要消除(不确定如何)。 我尝试使用grep -Po "(?<=\\().*(?=\\))" filelist
使用前瞻/外观,但该命令不起作用。
接下来我要做的是获取输出文件名并从它们所在的目录中删除它们。我不知道如何编写脚本,但它本质上会:
<returned results from grep>
rm dupe $target
rm somefile $target
您正在寻找的工具是xargs: http : //unixhelp.ed.ac.uk/CGI/man-cgi? xargs它在* nix系统上非常标准。
更新:鉴于目标等于文件所在的目录...
我相信语法看起来像:
yourgrepcmd | xargs -I{} rm "$target{}"
-I创建一个占位符字符串,grep命令中的每一行都插入到那里。
更新:
你需要删除parens的步骤是使用sed的替换命令( http://unixhelp.ed.ac.uk/CGI/man-cgi?sed )
像这样的东西:
cat filelist | sed "s/MD5 (\\([^)]*\\)) .*$/\\1/" | xargs -I{} rm "$target/{}"
这里故事的寓意是,如果你学会利用sed和xargs(或者如果你想要更高级的东西,那就是awk)你将成为一个更有能力的linux用户。
如果我理解正确,你想要采取这样的行
MD5 (dupe) = 1fb218dfef4c39b4c8fe740f882f351a
MD5 (somefile) = a5c6df9fad5dc4299f6e34e641396d38
提取没有括号的第二列以获取文件名
dupe
somefile
然后删除文件?
假设文件名没有空格,请尝试以下方法:
# this is where your duplicate files are.
dupe_directory='/some/path'
# Check that you found the right files:
awk '{print $2}' file-with-md5-lines.txt | tr -d '()' | xargs -I{} ls -l "$dupe_directory/{}"
# Looks ok, delete:
awk '{print $2}' file-with-md5-lines.txt | tr -d '()' | xargs -I{} rm -v "$dupe_directory/{}"
xargs -I{}
表示用xargs -I{}
替换参数(dupe filename),因此可以在更复杂的命令中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.