0

I am trying to concat all files with similar names but in different subdirectories.

I found this solution for exact file matches but I am not sure how to adapt it for partial matches

Create new concatenated files of same name in multiple directories

example file paths:

out/ch1/name1.1.assoc.txt
out/ch1/name2.1.assoc.txt
out/ch1/name3.1.assoc.txt

out/ch2/name1.2.assoc.txt
out/ch2/name2.2.assoc.txt
out/ch2/name3.2.assoc.txt

I have 20 different ch directories, and the final number in the file name matches the directory number.

I have a file with the names I am trying to concat.

name1 
name2
name3

How can I adapt this solution or concat this files in another way? End result I would like one directory with one file for each name in my file list (in this example the end result would be 3 files called name1, name2 and name3). we

2
  • It's not clear what you're trying to achieve. So you have different files, do you want them to be merged by mask (e.g. all name1.*.txt go to name1 regardless of directory) or by directory (e.g. all files in ch1 go to name1) or by some other way (e.g. all *1.assoc.txt go to name1)? Can you please add this information to your question?
    – rush
    Commented Dec 18, 2021 at 0:00
  • @rush I would like all name1.*.assoc.txt files to go to name1 regardless of directory. I edited the question for clarity
    – keenan
    Commented Dec 18, 2021 at 17:10

1 Answer 1

1

In zsh:

for name (${(f)"$(<names.txt)"})
  cat -- out/ch<->/$name.<->.assoc.txt(.n) > outdir/$name

In bash:

zsh -c '
  for name (${(f)"$(<names.txt)"})
    cat -- out/ch<->/$name.<->.assoc.txt(.n) > outdir/$name
'

If you can't install zsh, you can do something approaching in bash and on a GNU system with:

readarray -t files < <(grep . names.txt)
shopt -s extglob failglob
for name in "${files[@]}"; do
  printf '%s\0' out/ch+([0123456789])/"$name".+([0123456789]).assoc.txt |
    sort -zV |
    xargs -r0 cat -- > outdir/"$name"
done
6
  • I am using bash not zsh and I get the following bash: syntax error near unexpected token '(' on the first line
    – keenan
    Commented Dec 20, 2021 at 18:09
  • @keeman, yes zsh and bash are interpreters for two different languages. In bash, you'd run zsh -c 'the code as shown' Commented Dec 20, 2021 at 18:15
  • that results in the error bash: zsh: command not found
    – keenan
    Commented Dec 20, 2021 at 18:19
  • This works but it also copies all of the old files into outdir
    – keenan
    Commented Dec 22, 2021 at 16:55
  • @keenan, what old files? Commented Dec 22, 2021 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .