gpt4 book ai didi

linux - 在转储到文件之前处理从 bash 脚本中的 find 命令获取的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:35 25 4
gpt4 key购买 nike

我在 shell (bash) 脚本中使用 find 命令来获取与模式匹配的文件列表,并将这些文件名转储到文本文件中。

find ./ -type f -name *.txt >> files.list

生成一个包含

的文件
helloworld.txt
letter.txt
document-1.txt
document-1.backup.txt

等等

find 命令不是那么重要。我实际使用的那个包含 regex-es,并生成一个合理的匹配文件列表,用作程序的输入文件。

我想用一个标志标记每个文件名,以标记它是 Type A 文件还是 Type B 文件。即,我希望输出文件 files.list 看起来像这样

helloworld.txt type=A
letter.txt type=A
document-1.txt type=B
document-1.backup.txt type=B

如果 helloworldletterType A 文件,而 document-1Type B 文件。

我想也许我可以先写一个 files.list.tmp 文件,然后使用另一个 bash 脚本逐行处理它来读回它......但这会产生额外的文件和我真的不想那样做。

我可以使用 >>> 运算符来创建变量或其他东西吗?

我不太确定我应该在这里做什么。

顺便说一下,当这个 bash 脚本被调用时,它已经知道匹配正则表达式的文件是类型 A 还是类型 B,因为它作为脚本的参数给出。

最佳答案

I thought perhaps I could write a files.list.tmp file first, and then read it back in using another bash script processing it line by line... But that would make additional file and I don't really want to do that

您可以使用 process substitution 避免创建临时文件:

./ascript.sh < <(find . -type f -name '*.txt')

并让您的脚本逐行读取文件名。

关于linux - 在转储到文件之前处理从 bash 脚本中的 find 命令获取的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41241725/

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