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
scripting
add a comment |
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
scripting
1
Which field of the text file must4534534match 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
add a comment |
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
scripting
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
scripting
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 must4534534match 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
add a comment |
1
Which field of the text file must4534534match 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
add a comment |
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
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,2will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22
add a comment |
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
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 (settingtypset -u lastand then replacing${last^^}by just$lastshould 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
add a comment |
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
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,2will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22
add a comment |
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
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,2will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22
add a comment |
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
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
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,2will give you column 2 and column 5, but will not swap the order of them.
– Kusalananda
Jun 15 at 17:22
add a comment |
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,2will 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
add a comment |
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
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 (settingtypset -u lastand then replacing${last^^}by just$lastshould 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
add a comment |
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
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 (settingtypset -u lastand then replacing${last^^}by just$lastshould 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
add a comment |
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
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
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 (settingtypset -u lastand then replacing${last^^}by just$lastshould 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
add a comment |
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 (settingtypset -u lastand then replacing${last^^}by just$lastshould 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Which field of the text file must
4534534match 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