Linux GREP/SED [xargs] 在文件中查找模式和行号并使用 SED 删除 [英] Linux GREP/SED [xargs] find pattern and line number in files and remove with SED

查看:36
本文介绍了Linux GREP/SED [xargs] 在文件中查找模式和行号并使用 SED 删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是:

  1. 我有一个很长的文本字符串,其中包含特殊字符,它出现在各种文件的长行中.包含字符串的文件遍布整个文件系统 (Linux).该字符串可能位于它出现的文件中的不同行号上,但每个文件出现一次.但是,该字符串包含一个特定的模式,我可以通过键入该模式来识别整个字符串.

  1. I have a very long text string, containing special characters, which occurs on a long line in various files. Files containing the string are throughout the file system (Linux). The string may be on various line numbers in the files where it occurs, but occurs once per file. The string, however, contains a specific pattern which I can key on for identification of the entire string.

我知道如何通过使用 GREP -lir mystringprefix/path 键入字符串的前缀"来查找包含该字符串的文件的名称.

I know how to find the names of files containing the string by keying on the 'prefix' of the string using GREP -lir mystringprefix /path.

我知道如何使用 GREP 和 SED 确定文件中字符串的行号 grep -n "prefix" file.name |sed -n s/^([0-9])[:]./1/p".

I know how to determine the line number of the string in the file using GREP and SED using grep -n "prefix" file.name | sed -n "s/^([0-9])[:]./1/p".

我知道如何使用 SED 在文件中使用 sed -i xd/path/file(x 是要删除的行号)删除包含感兴趣字符串的行.

I know how to use SED to delete in place the line in the file containing the string of interest using sed -i xd /path/file (x is the line number to be deleted).

我的问题是如何将所有这些放在一起以确定哪些文件包含该模式,将名称传递给 GREP 以确定文件中的行号,然后将名称和行号传递给 SED 以删除该行?

My question is how can I put all this together to determine which files contain the pattern, pass the name to GREP to determine the line number in the file, then pass both the name and line number to SED to remove the line?

我需要这样做,从文件系统中的某个位置开始,然后递归搜索/删除.

I need to do this in such a way as to start in a certain location in the filesystem then have it search/delete recursively.

谢谢.

推荐答案

很简单:

for file in "$(grep -lir 'prefix' /path/)" ; do
    sed -ibk -e '/prefix/d' "$file"
done

我们假设 grep -lir prefix/path/ 返回一个以换行符分隔的文件名列表,并且您的前缀模式也是 sed 可以理解的.

Where we presume that grep -lir prefix /path/ returns a newline-delimited list of file names and that your prefix pattern is also something sed will understand.

这里我不需要确定行号,因为我告诉sed 找到匹配prefix 的行并直接删除它们.for file in "$(grep ...)" 构造有点不安全,但是,因为攻击者可以制作一个文件名,使其执行您不期望的操作.最好说:

I don't need to determine the line number here because I am telling sed to find lines that match prefix and delete them directly. The for file in "$(grep ...)" construct is somewhat unsafe, however, because an attacker could craft a filename that would cause it to do something you don't expect. It would be better to say:

grep -lir 'prefix' /path/ --null | while IFS= read -r -d '' file ; do
    sed -ibk -e '/prefix/d' "$file"
done

尽管请注意,在 while 循环体中分配的变量将无法生存.

Although note that variables assigned in the body of the while loop will not survive it.

这篇关于Linux GREP/SED [xargs] 在文件中查找模式和行号并使用 SED 删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆