[英]Iterating through pairs of files with glob
I'm having a difficult time trying to iterate through a long set of files that I need to pair up to run through some process.我很难尝试遍历一长串我需要配对才能运行某些过程的文件。 I'd like to generate a bit of a batch file, pairing each set of matching files one per line.
我想生成一些批处理文件,每行将每组匹配文件配对。 I've done this kind of thing before when it's a simple replacement (eg file1 = something.txt, file2 = something.csv).
当它只是一个简单的替换时,我以前做过这种事情(例如 file1 = something.txt,file2 = something.csv)。 But in this case, the end of the file string is a random UUID, and I can't figure out how to get bash to properly expand the glob the second file.
但在这种情况下,文件字符串的末尾是一个随机 UUID,我无法弄清楚如何获取 bash 以正确扩展第二个文件的 glob。
Given a directory of files like this:给定一个这样的文件目录:
banana_pre-proc_b101a65a-31c7-5e4f-b433-bac4fb1efc1f.txt
banana_proc_a75b3a3e-7140-1cb6-2ad1-c10f7db6743f.txt
cherry_pre-proc_f5d0716f-c205-b0b4-5c63-d33755767de4.txt
cherry_proc_025ff6d5-534d-0020-5446-5da3ed04adc6.txt
kiwi_pre-proc_26075f3b-e3a2-fc1a-a741-615cacfc1a7e.txt
kiwi_proc_be1760f6-413d-edc0-1efc-a134b1b6bfbb.txt
peach_pre-proc_ecafbb30-3df0-6014-61ee-11d1d5745b53.txt
peach_proc_bb3ea3fc-671e-e024-6e61-06a2bc147363.txt
pear_pre-proc_c2db376f-f351-7141-114e-a2ebc3cfc410.txt
pear_proc_ccb2f16a-27cd-c70d-7aac-ce72c3af6575.txt
How can I get a file that looks like:我怎样才能得到一个看起来像这样的文件:
banana_pre-proc_b101a65a-31c7-5e4f-b433-bac4fb1efc1f.txt banana_proc_a75b3a3e-7140-1cb6-2ad1-c10f7db6743f.txt
cherry_pre-proc_f5d0716f-c205-b0b4-5c63-d33755767de4.txt cherry_proc_025ff6d5-534d-0020-5446-5da3ed04adc6.txt
kiwi_pre-proc_26075f3b-e3a2-fc1a-a741-615cacfc1a7e.txt kiwi_proc_be1760f6-413d-edc0-1efc-a134b1b6bfbb.txt
peach_pre-proc_ecafbb30-3df0-6014-61ee-11d1d5745b53.txt peach_proc_bb3ea3fc-671e-e024-6e61-06a2bc147363.txt
pear_pre-proc_c2db376f-f351-7141-114e-a2ebc3cfc410.txt pear_proc_ccb2f16a-27cd-c70d-7aac-ce72c3af6575.txt
I thought I could do something like我以为我可以做类似的事情
for f in *pre-proc_*txt; do echo "$f" "${f/-pre-proc_/-proc_}"; done
But that doesn't deal with the UUID at the end of the file.但这不处理文件末尾的 UUID。 I've tried a few other iterations of this strategy too, but none get any closer.
我也尝试过此策略的其他一些迭代,但没有一个更接近。 What is the trick to doing this?
这样做的诀窍是什么? Obviously for a few files like this, I can just manually do it.
显然对于这样的几个文件,我可以手动完成。 But, the actual set of files I need to process is quite long and apart from just pulling them all into a text doc and then using some Vim macro or something, I'm a bit baffled as to how to get Bash to expand the glob like I'm intending.
但是,我需要处理的实际文件集非常长,除了将它们全部拉入文本文档然后使用一些 Vim 宏或其他东西之外,我对如何获取 Bash 来扩展 glob 有点困惑就像我打算的那样。
This seems to work:这似乎有效:
for preproc in *_pre-proc*; do
base=${preproc%_pre-proc*}
proc=${base}_proc*
echo $preproc $proc
done
We get a base name by stripping of the _pre_proc<uuid>
part, and then use the base name to find the matching _proc
file.我们通过剥离
_pre_proc<uuid>
部分得到一个基本名称,然后使用基本名称找到匹配的_proc
文件。
This I think should be sufficient:我认为这应该足够了:
printf "%s %s\n" *[-_]proc_*.txt
Glob expansions are sorted and the pairs of files share the same prefix. Glob 扩展被排序并且成对的文件共享相同的前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.