Score:0

How to move a matched id of mat files in different folder

cn flag

I have Matlab (.mat) files in a particular folder. In a text document, I have some IDs list marked as 0 and 1. First I need to match the IDs and separate the mat files that are marked as '1'. For your reference, I detailed it below with a sample set.

For instance: Assume that I have these files in a folder

2424903_ (1)_crop_vampire_3.1.2.mat
1905302 (2)_crop_vampire_3.1.2.mat
1904802 (1)_crop_vampire_3.1.2.mat
1902101_IYYANAR (1)_crop_vampire_3.1.2.mat

Text File:

2424903 - 1
1905302 - 0
1904802 - 0
1902101 - 1

Expected result:

2424903_ (1)_crop_vampire_3.1.2.mat
1902101_IYYANAR (1)_crop_vampire_3.1.2.mat

I need only these 2 files in a seperate folder.

I have tried below codes

$cat file.txt | xargs mv -t ./new folder
$xargs -a file.txt mv -t ./new folder

but I get errors like "No such file or directories" Kindly suggest a code/method to separate those files.

Score:0
vn flag

Try this script:

#!/bin/bash

while read -r line
do
  id_num=$(echo "$line" | cut -d ' ' -f 1)
  id_inc=$(echo "$line" | cut -d ' ' -f 3)
  if [[ "$id_inc" == 1 ]]; then
    file=$(find -name "${id_num}*")
    mv "$file" -t './new folder'
    # echo "$file" # if you want to print the filename
  fi
done < file.txt

There is an issue with Xargs and filenames containing spaces. This solution instead runs through each line, checks if the last field is 1, and in this case moves the file to new folder.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.