Script ‒ Move files that match with the records in text file based on parameters











up vote
0
down vote

favorite












What I'm trying to accomplish is to move files in my directory that match with the records in my text file based on 2 parameters.



For example I have a record in my text file that reads:



SPPARK|10416|3308123|3308123|Uphold|Thelma|1930/05/20||


I have a file in my directory that reads:



1123_M1123_UPHOLD_M1123_MESSAGE_SPPARK_348642.pdf


So if last name UPHOLD and 4th field M1123 match up to my fields in my text file, then I want to move them to a specified directory.



for files in test/* ; do
echo $files | awk -F "_" '{print $3,$4}'
done









share|improve this question




















  • 1




    Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
    – steeldriver
    Jun 14 at 13:10










  • The second field, and the last names aren't capitalized in the text file
    – Josh Adams
    Jun 14 at 13:12






  • 1




    @JoshAdams steeldriver asked about the name in the filename.
    – Kusalananda
    Jun 14 at 13:15










  • My bad, thank you, 4th field in the filename and they are always captalized
    – Josh Adams
    Jun 14 at 13:20










  • Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
    – Kusalananda
    Jun 15 at 17:28















up vote
0
down vote

favorite












What I'm trying to accomplish is to move files in my directory that match with the records in my text file based on 2 parameters.



For example I have a record in my text file that reads:



SPPARK|10416|3308123|3308123|Uphold|Thelma|1930/05/20||


I have a file in my directory that reads:



1123_M1123_UPHOLD_M1123_MESSAGE_SPPARK_348642.pdf


So if last name UPHOLD and 4th field M1123 match up to my fields in my text file, then I want to move them to a specified directory.



for files in test/* ; do
echo $files | awk -F "_" '{print $3,$4}'
done









share|improve this question




















  • 1




    Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
    – steeldriver
    Jun 14 at 13:10










  • The second field, and the last names aren't capitalized in the text file
    – Josh Adams
    Jun 14 at 13:12






  • 1




    @JoshAdams steeldriver asked about the name in the filename.
    – Kusalananda
    Jun 14 at 13:15










  • My bad, thank you, 4th field in the filename and they are always captalized
    – Josh Adams
    Jun 14 at 13:20










  • Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
    – Kusalananda
    Jun 15 at 17:28













up vote
0
down vote

favorite









up vote
0
down vote

favorite











What I'm trying to accomplish is to move files in my directory that match with the records in my text file based on 2 parameters.



For example I have a record in my text file that reads:



SPPARK|10416|3308123|3308123|Uphold|Thelma|1930/05/20||


I have a file in my directory that reads:



1123_M1123_UPHOLD_M1123_MESSAGE_SPPARK_348642.pdf


So if last name UPHOLD and 4th field M1123 match up to my fields in my text file, then I want to move them to a specified directory.



for files in test/* ; do
echo $files | awk -F "_" '{print $3,$4}'
done









share|improve this question















What I'm trying to accomplish is to move files in my directory that match with the records in my text file based on 2 parameters.



For example I have a record in my text file that reads:



SPPARK|10416|3308123|3308123|Uphold|Thelma|1930/05/20||


I have a file in my directory that reads:



1123_M1123_UPHOLD_M1123_MESSAGE_SPPARK_348642.pdf


So if last name UPHOLD and 4th field M1123 match up to my fields in my text file, then I want to move them to a specified directory.



for files in test/* ; do
echo $files | awk -F "_" '{print $3,$4}'
done






scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 14:13









Rui F Ribeiro

38.3k1475126




38.3k1475126










asked Jun 14 at 12:33









Josh Adams

246




246








  • 1




    Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
    – steeldriver
    Jun 14 at 13:10










  • The second field, and the last names aren't capitalized in the text file
    – Josh Adams
    Jun 14 at 13:12






  • 1




    @JoshAdams steeldriver asked about the name in the filename.
    – Kusalananda
    Jun 14 at 13:15










  • My bad, thank you, 4th field in the filename and they are always captalized
    – Josh Adams
    Jun 14 at 13:20










  • Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
    – Kusalananda
    Jun 15 at 17:28














  • 1




    Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
    – steeldriver
    Jun 14 at 13:10










  • The second field, and the last names aren't capitalized in the text file
    – Josh Adams
    Jun 14 at 13:12






  • 1




    @JoshAdams steeldriver asked about the name in the filename.
    – Kusalananda
    Jun 14 at 13:15










  • My bad, thank you, 4th field in the filename and they are always captalized
    – Josh Adams
    Jun 14 at 13:20










  • Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
    – Kusalananda
    Jun 15 at 17:28








1




1




Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
– steeldriver
Jun 14 at 13:10




Which field of the text file must 4534534 match with? is the lastname always capitalized in the filename?
– steeldriver
Jun 14 at 13:10












The second field, and the last names aren't capitalized in the text file
– Josh Adams
Jun 14 at 13:12




The second field, and the last names aren't capitalized in the text file
– Josh Adams
Jun 14 at 13:12




1




1




@JoshAdams steeldriver asked about the name in the filename.
– Kusalananda
Jun 14 at 13:15




@JoshAdams steeldriver asked about the name in the filename.
– Kusalananda
Jun 14 at 13:15












My bad, thank you, 4th field in the filename and they are always captalized
– Josh Adams
Jun 14 at 13:20




My bad, thank you, 4th field in the filename and they are always captalized
– Josh Adams
Jun 14 at 13:20












Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
– Kusalananda
Jun 15 at 17:28




Could you please give an example where the data in your file and the parts of the filename match up? Also, could you clarify whether you are looking for files based on the data, or whether you want to create the data from the filenames? I know your title says you want to move the files, but your loop makes no sense. Also clarify the field numbers, you refer the 4th field of the filename (M1123) as the "last name", which confuses me.
– Kusalananda
Jun 15 at 17:28










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Try this one. Change "echo" command to cp or mv after testing:



for f in samples.txt ; do 
echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path
done


I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:



awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt





share|improve this answer























  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14 at 13:04






  • 1




    Why are you looping over that single file name?
    – Kusalananda
    Jun 14 at 13:11










  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14 at 13:16






  • 1




    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15 at 17:22


















up vote
0
down vote













I would approach this the other way around:




  • read and split the text file into fields line-by-line

  • match and move the corresponding files


E.G. (assumes bash and GNU mv):



#!/bin/bash

shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do
echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt


Remove the echo once you are satisfied that it is doing what you want






share|improve this answer





















  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14 at 13:30










  • @JoshAdams what is your bash version (bash --version)? how are you executing the script?
    – steeldriver
    Jun 14 at 13:55










  • I'm using korn shell
    – Josh Adams
    Jun 14 at 13:59










  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
    – steeldriver
    Jun 14 at 14:19











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449800%2fscript-move-files-that-match-with-the-records-in-text-file-based-on-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Try this one. Change "echo" command to cp or mv after testing:



for f in samples.txt ; do 
echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path
done


I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:



awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt





share|improve this answer























  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14 at 13:04






  • 1




    Why are you looping over that single file name?
    – Kusalananda
    Jun 14 at 13:11










  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14 at 13:16






  • 1




    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15 at 17:22















up vote
0
down vote



accepted










Try this one. Change "echo" command to cp or mv after testing:



for f in samples.txt ; do 
echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path
done


I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:



awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt





share|improve this answer























  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14 at 13:04






  • 1




    Why are you looping over that single file name?
    – Kusalananda
    Jun 14 at 13:11










  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14 at 13:16






  • 1




    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15 at 17:22













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Try this one. Change "echo" command to cp or mv after testing:



for f in samples.txt ; do 
echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path
done


I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:



awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt





share|improve this answer














Try this one. Change "echo" command to cp or mv after testing:



for f in samples.txt ; do 
echo /source/path/*$(cut -d '|' -f 5,2 $f | tr '|'a-z '_'A-Z)*.pdf /destination/path
done


I'm sorry, my bad. Thanks @Kusalananda. Please, consider the above answer wrong and try this one instead. Supposing you are filtering on 5-th and 4-th columns it copies files from /src to /dst:



awk -F "|" '{system("cp /src/*"toupper($5)"_"$4"*.pdf /dst")}' samples.txt






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 16 at 20:26

























answered Jun 14 at 12:55









Mikhail Zakharov

2246




2246












  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14 at 13:04






  • 1




    Why are you looping over that single file name?
    – Kusalananda
    Jun 14 at 13:11










  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14 at 13:16






  • 1




    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15 at 17:22


















  • Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
    – Josh Adams
    Jun 14 at 13:04






  • 1




    Why are you looping over that single file name?
    – Kusalananda
    Jun 14 at 13:11










  • Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
    – Josh Adams
    Jun 14 at 13:16






  • 1




    cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
    – Kusalananda
    Jun 15 at 17:22
















Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
– Josh Adams
Jun 14 at 13:04




Thank You definitely what I'm looking, just one question, why does not exist show up at the end, I'm a noob to scripting
– Josh Adams
Jun 14 at 13:04




1




1




Why are you looping over that single file name?
– Kusalananda
Jun 14 at 13:11




Why are you looping over that single file name?
– Kusalananda
Jun 14 at 13:11












Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
– Josh Adams
Jun 14 at 13:16




Ok this won't work I tried copying, but It won't copy because the original filename is being changed, thus the file doesn't exist, I wish to retain the original file when copying
– Josh Adams
Jun 14 at 13:16




1




1




cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22




cut -f 5,2 will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22












up vote
0
down vote













I would approach this the other way around:




  • read and split the text file into fields line-by-line

  • match and move the corresponding files


E.G. (assumes bash and GNU mv):



#!/bin/bash

shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do
echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt


Remove the echo once you are satisfied that it is doing what you want






share|improve this answer





















  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14 at 13:30










  • @JoshAdams what is your bash version (bash --version)? how are you executing the script?
    – steeldriver
    Jun 14 at 13:55










  • I'm using korn shell
    – Josh Adams
    Jun 14 at 13:59










  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
    – steeldriver
    Jun 14 at 14:19















up vote
0
down vote













I would approach this the other way around:




  • read and split the text file into fields line-by-line

  • match and move the corresponding files


E.G. (assumes bash and GNU mv):



#!/bin/bash

shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do
echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt


Remove the echo once you are satisfied that it is doing what you want






share|improve this answer





















  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14 at 13:30










  • @JoshAdams what is your bash version (bash --version)? how are you executing the script?
    – steeldriver
    Jun 14 at 13:55










  • I'm using korn shell
    – Josh Adams
    Jun 14 at 13:59










  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
    – steeldriver
    Jun 14 at 14:19













up vote
0
down vote










up vote
0
down vote









I would approach this the other way around:




  • read and split the text file into fields line-by-line

  • match and move the corresponding files


E.G. (assumes bash and GNU mv):



#!/bin/bash

shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do
echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt


Remove the echo once you are satisfied that it is doing what you want






share|improve this answer












I would approach this the other way around:




  • read and split the text file into fields line-by-line

  • match and move the corresponding files


E.G. (assumes bash and GNU mv):



#!/bin/bash

shopt -s nullglob

while IFS='|' read -r loc a b c last first d e; do
echo mv --no-clobber -t path/to/target/ -- *_"$a"_"${last^^}"_*_*_*_*.pdf
done < file.txt


Remove the echo once you are satisfied that it is doing what you want







share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 14 at 13:18









steeldriver

33.7k34982




33.7k34982












  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14 at 13:30










  • @JoshAdams what is your bash version (bash --version)? how are you executing the script?
    – steeldriver
    Jun 14 at 13:55










  • I'm using korn shell
    – Josh Adams
    Jun 14 at 13:59










  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
    – steeldriver
    Jun 14 at 14:19


















  • Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
    – Josh Adams
    Jun 14 at 13:30










  • @JoshAdams what is your bash version (bash --version)? how are you executing the script?
    – steeldriver
    Jun 14 at 13:55










  • I'm using korn shell
    – Josh Adams
    Jun 14 at 13:59










  • @JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
    – steeldriver
    Jun 14 at 14:19
















Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
– Josh Adams
Jun 14 at 13:30




Ok I'm getting this error when I run it: 0403-011 The specified substitution is not valid for this command.
– Josh Adams
Jun 14 at 13:30












@JoshAdams what is your bash version (bash --version)? how are you executing the script?
– steeldriver
Jun 14 at 13:55




@JoshAdams what is your bash version (bash --version)? how are you executing the script?
– steeldriver
Jun 14 at 13:55












I'm using korn shell
– Josh Adams
Jun 14 at 13:59




I'm using korn shell
– Josh Adams
Jun 14 at 13:59












@JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
– steeldriver
Jun 14 at 14:19




@JoshAdams if you must do it in a Korn shell, then you will need to modify the upper-casing (setting typset -u last and then replacing ${last^^} by just $last should be a fairly portable alternative) and finding a way to do the nullglob - the latter will depend on which exact implementation of ksh you are using IIRC
– steeldriver
Jun 14 at 14:19


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449800%2fscript-move-files-that-match-with-the-records-in-text-file-based-on-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)