开发者

Expanding several filenames into one directory in bash

开发者 https://www.devze.com 2023-01-24 05:33 出处:网络
I want to run awk on several files.I have the filenames and a path to the files, but I can\'t开发者_运维问答 seem to connect the two.Here\'s what I have tried:

I want to run awk on several files. I have the filenames and a path to the files, but I can't开发者_运维问答 seem to connect the two. Here's what I have tried:


files=(a b c)
directory=/my/dir

awk $my_script "$directory/${files[@]}"

It awks the first file and leaves the rest alone. I'd rather not have to add the full path in my array (the values are used in several places). I think I want brace expansion, but it doesn't seem to work with arrays. What else could I do?


Using pattern substitution (# means something like ^ in regexps): ${files[@]/#/$directory/}


for i in /my/dir/[abc]; do
    awk $my_script "$i"
done

Or, if you want to actually just pass all of the file names to awk at once:

awk $my_script /my/dir/[abc]

If the file names are not actually single letters:

awk $my_script /my/dir/{file1,file2,file3,...}
0

精彩评论

暂无评论...
验证码 换一张
取 消