热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

删除文件-linuxcli中的字符串文件-Deletefileswithstringfoundinfile-linuxcli

IamtryingtodeleteerroneousemailsbasedonfindingtheemailaddressinthefileviaLinuxCLI.

I am trying to delete erroneous emails based on finding the email address in the file via Linux CLI.

我正在尝试删除错误的邮件,基于通过Linux CLI在文件中找到电子邮件地址。

I can get the files with

我可以把文件带来

find . | xargs grep -l email@domain.com

找到。| xargs grep -l email@domain.com

But I cannot figure out how to delete them from there as the following code doesn't work.

但是我不知道如何从那里删除它们,因为下面的代码不起作用。

rm -f | xargs find . | xargs grep -l email@domain.com

xargs rm -f | xargs。| xargs grep -l email@domain.com

Thank you for your assistance.

谢谢你的帮助。

7 个解决方案

#1


49  

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

为了安全起见,我通常将输出从find输出到awk,并创建一个批处理文件,其中每一行都是“rm filename”

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

这样,您就可以在实际运行它之前检查它,并手动修复使用regex难以处理的任何奇边情况

find . | xargs grep -l email@domain.com | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh

#2


53  

@Martin Beckett posted an excellent answer, please follow that guideline

@Martin Beckett发布了一个很好的答案,请遵循这个指导

solution for your command :

您的命令的解决方案:

grep -l email@domain.com * | xargs rm

Or

for file in $(grep -l email@domain.com *); do
    rm -i $file;
    #  ^ prompt for delete
done

#3


13  

You can use find's -exec and -delete, it will only delete the file if the grep command succeeds. Using grep -q so it wouldn't print anything, you can replace the -q with -l to see which files had the string in them.

您可以使用find的-exec和-delete,只有在grep命令成功时,它才会删除文件。使用grep -q,这样它就不会打印任何东西,您可以用-l替换-q,以查看哪些文件中有字符串。

find . -exec grep -q 'email@domain.com' '{}' \; -delete

#4


2  

Despite Martin's safe answer, if you've got certainty of what you want to delete, such as in writing a script, I've used this with greater success than any other one-liner suggested before around here:

尽管Martin给出了安全的答案,但如果您确定要删除的内容,比如在编写脚本时,我使用它的成功程度超过了之前这里提到的任何一行代码:

$ find . | grep -l email@domain.com | xargs -I {} rm -rf {}

But I rather find by name:

但我宁愿按名字来找:

$ find . -iname *something* | xargs -I {} echo {}

#5


0  

find . | xargs grep -l email@domain.com

how to remove:

如何删除:

rm -f 'find . | xargs grep -l email@domain.com'

#6


0  

rm -f `find . | xargs grep -li email@domain.com`

does the job better. Use `...` to run the command to offer the file names containing email.@domain.com (grep -l lists them, -i ignores case) to remove them with rm (-f forcibly / -i interactively).

更好的发挥作用。使用“…“运行命令,提供包含电子邮件。@domain.com (grep -l列出它们,我忽略大小写)的文件名,并使用rm (-f force / -i交互性地删除它们)删除它们。”

#7


0  

I liked Martin Beckett's solution but found that file names with spaces could trip it up (like who uses spaces in file names, pfft :D). Also I wanted to review what was matched so I move the matched files to a local folder instead of just deleting them with the 'rm' command:

我喜欢Martin Beckett的解决方案,但发现有空格的文件名可能会出错(比如谁在文件名中使用空格,pfft:D)。我还想回顾一下匹配的内容,所以我将匹配的文件移动到一个本地文件夹,而不是用“rm”命令删除它们:

# Make a folder in the current directory to put the matched files
$ mkdir -p './matched-files'

# Create a script to move files that match the grep
# NOTE: Remove "-name '*.txt'" to allow all file extensions to be searched.
# NOTE: Edit the grep argument 'something' to what you want to search for.

$ find . -name '*.txt' -print0 | xargs -0 grep -al 'something' | awk -F '\n' '{ print "mv \""$0"\" ./matched-files" }' > doit.sh

Or because its possible (in Linux, idk about other OS's) to have newlines in a file name you can use this longer, untested if works better (who puts newlines in filenames? pfft :D), version:

$ find . -name '*.txt' -print0 | xargs -0 grep -alZ 'something' | awk -F '\0' '{ for (x=1; x doit.sh

# Evaluate the file following the 'source' command as a list of commands executed in the current context:
$ source doit.sh

NOTE: I had issues where grep could not match inside files that had utf-16 encoding. See here for a workaround. In case that website disappears what you do is use grep's -a flag which makes grep treat files as text and use a regex pattern that matches any first-byte in each extended character. For example to match Entité do this:

注意:在具有utf-16编码的文件中,我遇到了grep无法匹配的问题。请看这里的变通方法。如果这个网站消失了,你要做的就是使用grep -a标志,使grep将文件当作文本,并使用regex模式来匹配每个扩展字符中的任何第一个字节。例如,要匹配Entite,请这样做:

grep -a 'Entit.e'

and if that doesn't work then try this:

如果没有效果,试试这个:

grep -a 'E.n.t.i.t.e'

推荐阅读
  • C语言注释工具及快捷键,删除C语言注释工具的实现思路
    本文介绍了C语言中注释的两种方式以及注释的作用,提供了删除C语言注释的工具实现思路,并分享了C语言中注释的快捷键操作方法。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • 本文介绍了Linux Shell中括号和整数扩展的使用方法,包括命令组、命令替换、初始化数组以及算术表达式和逻辑判断的相关内容。括号中的命令将会在新开的子shell中顺序执行,括号中的变量不能被脚本余下的部分使用。命令替换可以用于将命令的标准输出作为另一个命令的输入。括号中的运算符和表达式符合C语言运算规则,可以用在整数扩展中进行算术计算和逻辑判断。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Gitlab接入公司内部单点登录的安装和配置教程
    本文介绍了如何将公司内部的Gitlab系统接入单点登录服务,并提供了安装和配置的详细教程。通过使用oauth2协议,将原有的各子系统的独立登录统一迁移至单点登录。文章包括Gitlab的安装环境、版本号、编辑配置文件的步骤,并解决了在迁移过程中可能遇到的问题。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 总结一下C中string的操作,来自〈CPrimer〉第四版。1.string对象的定义和初始化:strings1;空串strings2(s1);将s2初始 ... [详细]
  • 初探PLC 的ST 语言转换成C++ 的方法
    自动控制软件绕不开ST(StructureText)语言。它是IEC61131-3标准中唯一的一个高级语言。目前,大多数PLC产品支持ST ... [详细]
  • Ihavebeenworkingwithbufferingafileonmylocaldrivetoparseandobtaincertaindata.Forte ... [详细]
  • 三、查看Linux版本查看系统版本信息的命令:lsb_release-a[root@localhost~]#lsb_release-aLSBVersion::co ... [详细]
  • 1、PLSQLDeveloper记住登陆密码在使用PLSQLDeveloper时,为了工作方便希望PLSQLDeveloper记住登录Oracle的用户名和密码&#x ... [详细]
author-avatar
林泳_钿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有