Home > Enterprise >  How can I match two sections of a filename to list the files that match it?
How can I match two sections of a filename to list the files that match it?

Time:01-28

I have a bunch of files that need merging. The name of the files are something like this-

JOHN_80_xyz_yeti.txt
JOHN_80_xyz_puma.txt
JOHN_80_def_yeti.txt
JOHN_80_def_puma.txt
JOHN_81_xyz_yeti.txt
JOHN_81_xyz_puma.txt
JOHN_81_def_yeti.txt
JOHN_81_def_puma.txt
JOHN_82_xyz_yeti.txt
JOHN_82_xyz_puma.txt
JOHN_82_def_yeti.txt
JOHN_82_def_puma.txt
JOHN_83_xyz_yeti.txt
JOHN_83_xyz_puma.txt
JOHN_83_def_yeti.txt
JOHN_83_def_puma.txt

I want to merge JOHN_80_xyz_yeti.txt and JOHN_80_def_yeti.txt; JOHN_80_xyz_puma.txt and JOHN_80_def_puma.txt; JOHN_81_xyz_yeti.txt and JOHN_81_def_yeti.txt; JOHN_81_xyz_puma.txt and JOHN_81_def_puma.txt; and so forth, recursively through my files in a bash for loop. What command can I use so that it finds the files that have "80" and "yeti" together and list/echo it as a variable to be used in a for loop?

The command that I want to use these files for is given below-

merge -1 JOHN_80_xyz_yeti.txt -2 JOHN_80_def_yeti.txt > merged.JOHN_80_yeti.txt

merge -1 JOHN_80_xyz_puma.txt -2 JOHN_80_def_puma.txt > merged.JOHN_80_puma.txt

I tried "find file name" but failed to get the desired results.

CodePudding user response:

Loop through all the xyz files and use string substitution to replace xyz with def, and replace the entire suffix with chewbacca.

for xyzfile in *_xyz_*.txt; do
    deffile=${file/_xyz_/_def_}
    result=${file/_xyz_*/_chewbaccca.txt}
    merge -1 "$xyzfile" -2 "$deffile" > "$result"

CodePudding user response:

Assuming you want to dynamically collect the substrings -

mkdir -p merged                    # backup
for f in JOHN*txt                  # loop though glob
do IFS=_ read a b x c <<< "$f"     # parse the filename
   lst=( $a*$b*$c )                # get its match (always only one?)
   [[ -e $lst ]] || continue       # skip if already done (this is 2nd file)
   merge -1 "${lst[0]}" -2 "${lst[1]}" > merged.${a}_${b}_${c} # do the thing.
   mv "${lst[@]}" merged/          # move to backup
done

You can verify and then delete the merged folder, or undo/edit/redo till it's right.

  • Related