检索“TODO”文本文件中的行

发布于 2024-10-04 10:52:40 字数 398 浏览 7 评论 0原文

我正在 GEdit(文本编辑器)中编辑一个项目。当我输入 TODO 时,它会突出显示为黄色以供将来参考。我周围有很多这样的TODO

为了更清楚地说明我需要做什么,任何人都可以向我展示一种从一堆文件中提取任何 TODO 行并将它们放入一个名为 TODOs.txt 的文本文件中的方法。 txt ?

我有这样的东西:

// TODO:错误处理。

并希望将其放入这样的文件中:

* <文件名>; <行号>错误处理

Linux 应用程序(CLI、GUI 不介意)会更好,但正则表达式脚本或其他人可能想出的方法会很酷。

I'm editing a project in GEdit (text editor). When I type TODO it highlights it yellow for future reference. I have quite a few of these TODOs around the place.

To make it clearer what I need to do, can anyone show me a way to extract any TODO lines from a bunch of files and put them into one text file called, for example, TODOs.txt?

I have something like this:

// TODO: Error handling.

And want it to be put in a file like this:

* <file name> <line number> Error handling

A Linux application (CLI, GUI don't mind) would be preferable, but a regex script or another method someone could come up with would be cool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

几味少女 2024-10-11 10:52:40

尝试 grep TODO -rnf * > TODOs.txt

try grep TODO -rnf * > TODOs.txt

温柔一刀 2024-10-11 10:52:40

如果文件是 git 版本控制的,您可能需要重用 git 的 grep。要在源文件中 grep 查找 TODO,请在存储库的根目录中键入以下命令

git grep TODO

该命令输出如下所示:

[~/projects/atl][ros] > git grep TODO
atl_gazebo/src/plugins/quadrotor_gplugin.cpp:  // TODO: switch to attitude controller when in offboard mode

要包含行号,请添加 -n 标志:

[~/projects/atl][ros] > git grep -n TODO
atl_gazebo/src/plugins/quadrotor_gplugin.cpp:92:  // TODO: switch to attitude controller when in offboard mode

有关更多选项,文档为 < a href="https://git-scm.com/docs/git-grep" rel="nofollow noreferrer">此处。

If the file is git version controlled, you might want to reuse git's grep. To grep for TODOs in your source files you type the following command at the root of the repo

git grep TODO

The command outputs something like this:

[~/projects/atl][ros] > git grep TODO
atl_gazebo/src/plugins/quadrotor_gplugin.cpp:  // TODO: switch to attitude controller when in offboard mode

To include the line number add the -n flag:

[~/projects/atl][ros] > git grep -n TODO
atl_gazebo/src/plugins/quadrotor_gplugin.cpp:92:  // TODO: switch to attitude controller when in offboard mode

For more options docs are here.

小…红帽 2024-10-11 10:52:40

如果包含 TODO 的文件列表存储在一个名为“file_list.txt”的文件中,请运行:

grep -n `cat file_list.txt` > TODOs.txt

这将检索包含“TODO”字符串的所有行的列表,前面加上文件名和行号,并存储TODOs.txt 中的内容

If your list of files which has TODO in them is stored in a file, say named "file_list.txt", run:

grep -n `cat file_list.txt` > TODOs.txt

This will retrieve a list of all the lines containing "TODO" string, prepended with filename and line #, and store that in TODOs.txt

只想待在家 2024-10-11 10:52:40

Riley Lark(和 EdoDodo)的答案可能是最简洁的,但是为了不在输出中显示字面的“TODO”文本,您可以使用 ack

ack --no-group 'TODO (.*)' --output=' $1' > TODOs.txt

输出:

lib/Example.pm:102: Move _cmd out into a separate role
lib/Example.pm:364: Add a filename parameter
lib/Example2.pm:45: Move comment block to POD format

如果您想要一些稍微不同的格式,那么添加 --no-group 选项将提供:

lib/Example.pm
102: Move Move _cmd out into a separate role
364: Add a filename parameter

lib/Example2.pm
45: Move comment block to POD format

Riley Lark's (and EdoDodo's) answers are probably the most concise, however to not have the literal "TODO" text to show up in the output, you could use ack:

ack --no-group 'TODO (.*)' --output=' $1' > TODOs.txt

Output:

lib/Example.pm:102: Move _cmd out into a separate role
lib/Example.pm:364: Add a filename parameter
lib/Example2.pm:45: Move comment block to POD format

If you wanted some slightly different formatting then adding the --no-group option would provide:

lib/Example.pm
102: Move Move _cmd out into a separate role
364: Add a filename parameter

lib/Example2.pm
45: Move comment block to POD format
恬淡成诗 2024-10-11 10:52:40

这将在当前目录下的任何文件中递归地查找任何带有“// TODO”的行:

grep -rn '// *TODO' .

这会稍微清理输出:

grep -rn '// *TODO' . | while read l; do
        echo "$(cut -d : -f 1,2 <<<"$l"):$(sed 's|.*//[[:space:]]*TODO||' <<<"$l")"
done

请注意,如果源代码文件名中存在冒号(:),这可能会中断。

您可以将输出放入具有最终重定向的文件中。

编辑:

这更好:

grep -rn '// *TODO' . | sed 's|^\([^:]*:[^:]*:\).*//[[:space:]]*TODO|\1|'

This will recursively find any line with '// TODO' in any file under the current directory:

grep -rn '// *TODO' .

This cleans up the output a bit:

grep -rn '// *TODO' . | while read l; do
        echo "$(cut -d : -f 1,2 <<<"$l"):$(sed 's|.*//[[:space:]]*TODO||' <<<"$l")"
done

Note that this might break if there are colons (:) in your source code file names.

You can put the output in a file with a final redirection.

EDIT:

This is even better:

grep -rn '// *TODO' . | sed 's|^\([^:]*:[^:]*:\).*//[[:space:]]*TODO|\1|'
吝吻 2024-10-11 10:52:40

这是一个命令,它将查找您所在目录及其子目录中的所有 TODO,并将它们放入文本文件中:

grep 'TODO' -rn * > TODOs.txt

编辑:

要删除之前不必要的输出,您可以使用此 命令命令改为:

grep 'TODO.*' -rno * > TODOs.txt

Here's a command that will find all the TODOs in the directory you're in, and it's subdirectories, and put them in a text file:

grep 'TODO' -rn * > TODOs.txt

EDIT:

To remove the unnecessary output before it, you could use this command instead:

grep 'TODO.*' -rno * > TODOs.txt
—━☆沉默づ 2024-10-11 10:52:40

遇到了同样的问题,所以想出了一个简单的 python 脚本来使用 git grep 来完成它......

import os
import subprocess
from contextlib import suppress

result = subprocess.run("git grep -l TODO", stdout=subprocess.PIPE)
lst = result.stdout.decode().split("\n")

with suppress(Exception): lst.remove(os.path.basename(__file__)) # remove this filename
with suppress(Exception): lst.remove("") # remove empty filename

todos = []
for file in lst:
    with open(file) as reader:
        for idx, row in enumerate(reader):
            if "TODO" in row: todos.append((file, idx, row))

for todo in todos:
    print(todo)

Hade the same issue so came up with a simple python script to do it using git grep...

import os
import subprocess
from contextlib import suppress

result = subprocess.run("git grep -l TODO", stdout=subprocess.PIPE)
lst = result.stdout.decode().split("\n")

with suppress(Exception): lst.remove(os.path.basename(__file__)) # remove this filename
with suppress(Exception): lst.remove("") # remove empty filename

todos = []
for file in lst:
    with open(file) as reader:
        for idx, row in enumerate(reader):
            if "TODO" in row: todos.append((file, idx, row))

for todo in todos:
    print(todo)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文