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

标签 linux string bash shell find

我在 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/

相关文章:

regex - 列出名称中包含两个连续元音字母的目录。 Linux终端命令

linux - Docker容器中的Docker未映射.ssh卷

Ruby:如何使用存储在数组中的数组名称

php - 如何在 PHP 中将字符串从多行转换为单行?

linux - Linux 守护进程中的高效文件检查

c - 如何检查C中的缓冲区?

linux - Git:如何列出所有试图提交的(暂存的)文件

java - 如何在java中的字符串中附加反斜杠

linux - Bash:日期 "%b"和日期 "%h"(当然还有)日期 "%B"给出月份的全名?

linux - GNU BASH 命令更改图像大小和 DPI?