gpt4 book ai didi

regex - 查找 "StringBetween"整个目录 linux cli

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:41 25 4
gpt4 key购买 nike

我有一个运行着 iredmail 的邮件服务器。

我们每天发送几封时事通讯,“退回”的在 vmail 文件夹中。

我想在不使用 smtp 的情况下从这些文件中提取“id”,因为我已经在本地拥有整个邮件内容。

"http://www.myserver.com/index.php?m=xxxxxxxxxx"

我现在想遍历目录中的所有文件并在每个文件中找到第一次出现的“xxxxxxxxx”并将其保存到文本文件中,这样我就可以从邮件列表数据库中删除这些 ID。

如果可能,通过 CLI。

提前致谢。

最佳答案

尝试使用下面的命令来查找每个文件中第一次出现的 id,

find . -maxdepth 1 -type f -exec grep -oP -m 1 '(?<=index\.php\?m\=)[^"]*' {} \; | sort -u

解释:

find         # command to find files are directories

. # current directory(path on which the operation of find command is going to takes place)

-maxdepth 1 # don't search inside subdirectories.(ie; search only in the current directory)

-type f # Only files

-exec grep -oP -m 1 '(?<=index\.php\?m\=)[^"]*' {} \; # execute the grep command on only the founded files.

-oP # (-o)print only the match,(-P) Perl-regex.
-m 1 # Grep to stop after first match occurs on each file.
(?<=index\.php\?m\=)[^"]* # A lookbehind is used here. It matches the text after `index.php?m=` upto the first occurrence of `"`. It helps to match the id's.
{} # Founded files.
\; # stops the find command.

sort -u # print only the unique id's.

关于regex - 查找 "StringBetween"整个目录 linux cli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24276289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com