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

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

问题描述

我的情况是:


  1. 我有一个非常长的文本字符串,包含特殊字符,在各种文件中。包含字符串的文件遍布整个文件系统(Linux)。该字符串可能位于文件所在文件中的各种行号上,但每个文件只会出现一次。然而,字符串包含一个特定的模式,我可以通过它来识别整个字符串。

  2. 我知道如何找到包含字符串通过使用GREP -lir mystringprefix / path键入字符串的'前缀'。
  3. 我知道如何确定字符串的行号文件使用GREP和SED使用grep -nprefixfile.name | sed -ns / ^([0-9] )[:]。 / \ 1 / p。


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


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



我需要这样做,以便在文件系统的某个位置启动,然后让它搜索/删除递归。



谢谢。

解决方案

很简单:在$(grep -lir'prefix'/ path /)中为文件添加文件

 ;做
sed -ibk -e'/ prefix / d'$ file
完成

假定 grep -lir前缀/ path / 返回一个换行符分隔的文件名列表,并且您的前缀模式也是 sed 会理解。



我不需要在这里确定行号,因为我告诉 sed 查找匹配前缀的行并直接删除它们。然而,$(grep ...)构造文件中的文件在某种程度上是不安全的,因为攻击者可以制作一个文件名,没想到。最好是说:

$ p $ grep -lir'prefix'/ path / --null |而IFS =读取-r -d''文件;做
sed -ibk -e'/ prefix / d'$ file
完成

虽然请注意,在主体中分配的变量在循环中将无法存活。


My situation is:

  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.

  2. 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.

  3. 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".

  4. 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).

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.

Thank you.

解决方案

Pretty simple:

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

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.

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

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

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

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