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
我正在编写一个每分钟运行几次的bash脚本。我想要它做的是找到指定目录中包含指定字符串的所有文件,并搜索该文件列表并删除以不同特定字符串开头的任何行(在这种情况下,它是
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'
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.
我在头部-10中添加的唯一原因是,每次脚本运行时,它将仅从查看最近的10个文件开始。我不希望它花费大量时间在整个目录中进行不必要的搜索,因为它将通过并每分钟删除一次。
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?
该脚本必须运行在与文件不同的目录中。此外,如果文件中不存在“ ”字符串,文件上的修改日期是否会更改?
2 个解决方案
#1
1
There are a variety of problem with this part of the command...
这部分命令存在各种问题......
ls -1t /the/directory | head -10 | grep -l "qualifying string" * ...
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.
首先,您似乎试图管道ls的输出... |将-10转入grep,这会导致grep在ls的输出中搜索“qualifying string”。除此之外,您转过来并将*作为命令行参数提供给grep,使其搜索所有文件,并完全忽略ls和head命令。
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:
您可能想要阅读有关xargs命令的信息,该命令读取stdin上的文件列表,然后针对该列表运行给定命令。例如,您应该能够像这样生成文件列表:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string"
And to apply sed
to those files:
并将sed应用于这些文件:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string" |
sed -i 's/something/else/g'
Modifying the files with sed
will update the modification time on the files.
使用sed修改文件将更新文件的修改时间。
#2
0
You can use globbing with the *
character to expand file names and loop through the directory.
您可以使用带有*字符的globbing来扩展文件名并循环遍历目录。
n=0
for file in /the/directory/*; do
if [ -f "$file" ]; then
grep "qualifying string" "$file" && sed -i '/^<meta/d' "$file"
n=$((n+1))
fi
[ $n -eq 10 ] && break
done
#1
1
There are a variety of problem with this part of the command...
这部分命令存在各种问题......
ls -1t /the/directory | head -10 | grep -l "qualifying string" * ...
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.
首先,您似乎试图管道ls的输出... |将-10转入grep,这会导致grep在ls的输出中搜索“qualifying string”。除此之外,您转过来并将*作为命令行参数提供给grep,使其搜索所有文件,并完全忽略ls和head命令。
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:
您可能想要阅读有关xargs命令的信息,该命令读取stdin上的文件列表,然后针对该列表运行给定命令。例如,您应该能够像这样生成文件列表:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string"
And to apply sed
to those files:
并将sed应用于这些文件:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string" |
sed -i 's/something/else/g'
Modifying the files with sed
will update the modification time on the files.
使用sed修改文件将更新文件的修改时间。
#2
0
You can use globbing with the *
character to expand file names and loop through the directory.
您可以使用带有*字符的globbing来扩展文件名并循环遍历目录。
n=0
for file in /the/directory/*; do
if [ -f "$file" ]; then
grep "qualifying string" "$file" && sed -i '/^<meta/d' "$file"
n=$((n+1))
fi
[ $n -eq 10 ] && break
done