试图删除与从文件中的特定字符串所在的文件满足目标条件开头的行,在bash / Linux的 [英] Trying to delete lines beginning with a specific string from files where the file meets a target condition, in bash/linux

查看:81
本文介绍了试图删除与从文件中的特定字符串所在的文件满足目标条件开头的行,在bash / Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个bash脚本,将运行几次一分钟。我想它做的是找到包含指定字符串的指定目录下的所有文件,搜索文件的列表并且删除与不同的特定字符串开头的任何行(在这种情况下,它是

下面就是我试过到目前为止S,但他们没有工作:

I am writing a bash script that will run a couple of times a minute. What I would like it to do is find all files in a specified directory that contain a specified string, and search that list of files and delete any line beginning with a different specific string (in this case it's

Here's what I've tried s far, but they aren't working:

ls -1t /the/directory | head -10 | grep -l "qualifying string" * | sed -i '/^<meta/d' *'
ls -1t /the/directory | head -10 | grep -l "qualifying string" * | sed -i '/^<meta/d' /the/directory'

我的头-10增加的唯一原因是让每一个脚本运行时,它将仅看最近10个文件开始。我不希望它花了很多时间在整个目录,因为它将经历和删除行多次分钟不必要的搜索。

The only reason I added in the head -10 is so that every time the script runs, it will start by only looking at the 10 most recent files. I don't want it to spend a lot of time searching needlessly through the entire directory since it will be going through and removing the line many times a minute.

该脚本已被用完了不同的目录中的文件中另外,将在这些文件的修改日期更改,如果&LT;元。字符串不文件中存在吗?

The script has to be run out of a different directory than the files are in. Also, would the modified date on the files change if the "<meta" string doesn't exist in the file?

推荐答案

有各种问题与命令的这一部分...

There are a variety of problem with this part of the command...

ls -1t /the/directory | head -10 | grep -l "qualifying string" * ...

首先,你似乎是想管 LS的输出... |头-10 成的grep,这将导致的grep 来搜索 LS的输出。除了然后你转身并提供 * 作为命令行参数到grep,使其中的所有文件进行搜索,而完全忽略了 LS 命令。

First, you appear to be trying to pipe the output of ls ... | head -10 into grep, which would cause grep to search for "qualifying string" in the output of ls. Except then you turn around and provide * as a command line argument to grep, causing it to search in all the files, and completely ignoring the ls and head commands.

您可能想阅读有关的xargs 命令,其内容上的标准输入的文件列表,然后运行针对该列表中的给定的命令。例如,你应该能够产生这样的文件列表:

You probably want to read about the xargs commands, which reads a list of files on stdin and then runs a given command against that list. For example, you ought to be able to generate your file list like this:

ls -1t /the/directory | head -10 | 
  xargs grep -l "qualifying string"

和适用 SED 对这些文件:

ls -1t /the/directory | head -10 | 
  xargs grep -l "qualifying string" |
  sed -i 's/something/else/g'

修改为 SED 文件将更新文件的修改时间。

Modifying the files with sed will update the modification time on the files.

这篇关于试图删除与从文件中的特定字符串所在的文件满足目标条件开头的行,在bash / Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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