Hello,
I have files like this
img.txt
./Rearrange_zoca/fitted_data/WX226_05b_Ncere.jpg
./Rearrange_zoca/fitted_data/w322_03B_60_70.jpg
./Rearrange_zoca/fitted_data/wrx226_12A_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_01A_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_02A_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_04B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_05B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_06A_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_07A_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_08B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_11B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_15B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_20B_50_60.jpg
./Rearrange_zoca/fitted_data/wx124_21B_50_60.jpg
./Rearrange_zoca/fitted_data/wx226_04B_50_60.jpg
./Rearrange_zoca/fitted_data/wx226_04B_Ncere.jpg
./Rearrange_zoca/fitted_data/wx226_05B_50_60.jpg
./Rearrange_zoca/fitted_data/wx226_09A_50_60.jpg
./Rearrange_zoca/fitted_data/wx226_09A_Ncere.jpg
./Rearrange_zoca/fitted_data/wx322-04A_50_60.jpg
./Rearrange_zoca/fitted_data/wx322_01A_50_60.jpg
./Rearrange_zoca/fitted_data/wx322_01A_Ncere.jpg
./Rearrange_zoca/fitted_data/wx322_02A_50_60.jpg
./Rearrange_zoca/fitted_data/wx322_02A_Ncere.jpg
./Rearrange_zoca/fitted_data/wx322_04A_Ncere.jpg
./Rearrange_zoca/fitted_data/wx322_05B_50_60.jpg
they need to be moved to their specific directories which look like
petdir.txt
./1/zoca_226-04B_JPR/zoca_226-04B_JPR_PET
./1/zoca_226-05B_DMP/zoca_226-05B_DMP_PET
./1/zoca_226-09A_J-L/zoca_226-09A_J-L_PET
./1/zoca_226-12A_ATH/zoca_226-12A_ATH_PET
./1/zoca_322-01A_WSL/zoca_322-01A_WSL_PET
./1/zoca_322-02A_ECB/zoca_322-02A_ECB_PET
./1/zoca_322-03B_KSJ/zoca_322-03B_KJ_PET
./1/zoca_322-04A_J-K/zoca_322-04A_JK_PET
./1/zoca_322-05B-L-S/zoca_322-05B-L-S_PET
./2/zoca_124-01A_KMT/zoca_124-01A_KMT_PET
./2/zoca_124-02A_KJM/zoca_124-02A_KJM_PET
./2/zoca_124-04B_DRC/zoca_124-04B_DRC_PET
./2/zoca_124-05B_LAB/zoca_124-05B_LAB_PET
./2/zoca_124-06A_PLD/zoca_124-06A_PLD_PET
./2/zoca_124-07A_CCR/zoca_124-07A_CCR_PET
./2/zoca_124-08B_DYW/zoca_124-08B_DYW_PET
./2/zoca_124-11B_JKR/zoca_124-11B_JKR_PET
./2/zoca_124-15B_LAM/zoca_124-15B_LAM_PET
./2/zoca_124-20B_C-T/zoca_124-20B_C-T_PET
./2/zoca_124-21B_KWB/zoca_124-21B_KWB_PET
the patterns that need to be matched in these two files are
grep -o -i '[0-9]\{3\}[-_][0-9]\{2\}[Ab]' img.txt
awk -F"zoca_" '{print substr($2,1,7)}' petdir.txt
Thanks in advance
The following Perl script prints the appropriate "mv" commands given the two files as input -
$
$
$ cat -n img.txt
1 ./Rearrange_zoca/fitted_data/WX226_05b_Ncere.jpg
2 ./Rearrange_zoca/fitted_data/w322_03B_60_70.jpg
3 ./Rearrange_zoca/fitted_data/wrx226_12A_50_60.jpg
4 ./Rearrange_zoca/fitted_data/wx124_01A_50_60.jpg
5 ./Rearrange_zoca/fitted_data/wx124_02A_50_60.jpg
6 ./Rearrange_zoca/fitted_data/wx124_04B_50_60.jpg
7 ./Rearrange_zoca/fitted_data/wx124_05B_50_60.jpg
8 ./Rearrange_zoca/fitted_data/wx124_06A_50_60.jpg
9 ./Rearrange_zoca/fitted_data/wx124_07A_50_60.jpg
10 ./Rearrange_zoca/fitted_data/wx124_08B_50_60.jpg
11 ./Rearrange_zoca/fitted_data/wx124_11B_50_60.jpg
12 ./Rearrange_zoca/fitted_data/wx124_15B_50_60.jpg
13 ./Rearrange_zoca/fitted_data/wx124_20B_50_60.jpg
14 ./Rearrange_zoca/fitted_data/wx124_21B_50_60.jpg
15 ./Rearrange_zoca/fitted_data/wx226_04B_50_60.jpg
16 ./Rearrange_zoca/fitted_data/wx226_04B_Ncere.jpg
17 ./Rearrange_zoca/fitted_data/wx226_05B_50_60.jpg
18 ./Rearrange_zoca/fitted_data/wx226_09A_50_60.jpg
19 ./Rearrange_zoca/fitted_data/wx226_09A_Ncere.jpg
20 ./Rearrange_zoca/fitted_data/wx322-04A_50_60.jpg
21 ./Rearrange_zoca/fitted_data/wx322_01A_50_60.jpg
22 ./Rearrange_zoca/fitted_data/wx322_01A_Ncere.jpg
23 ./Rearrange_zoca/fitted_data/wx322_02A_50_60.jpg
24 ./Rearrange_zoca/fitted_data/wx322_02A_Ncere.jpg
25 ./Rearrange_zoca/fitted_data/wx322_04A_Ncere.jpg
26 ./Rearrange_zoca/fitted_data/wx322_05B_50_60.jpg
$
$ cat -n petdir.txt
1 ./1/zoca_226-04B_JPR/zoca_226-04B_JPR_PET
2 ./1/zoca_226-05B_DMP/zoca_226-05B_DMP_PET
3 ./1/zoca_226-09A_J-L/zoca_226-09A_J-L_PET
4 ./1/zoca_226-12A_ATH/zoca_226-12A_ATH_PET
5 ./1/zoca_322-01A_WSL/zoca_322-01A_WSL_PET
6 ./1/zoca_322-02A_ECB/zoca_322-02A_ECB_PET
7 ./1/zoca_322-03B_KSJ/zoca_322-03B_KJ_PET
8 ./1/zoca_322-04A_J-K/zoca_322-04A_JK_PET
9 ./1/zoca_322-05B-L-S/zoca_322-05B-L-S_PET
10 ./2/zoca_124-01A_KMT/zoca_124-01A_KMT_PET
11 ./2/zoca_124-02A_KJM/zoca_124-02A_KJM_PET
12 ./2/zoca_124-04B_DRC/zoca_124-04B_DRC_PET
13 ./2/zoca_124-05B_LAB/zoca_124-05B_LAB_PET
14 ./2/zoca_124-06A_PLD/zoca_124-06A_PLD_PET
15 ./2/zoca_124-07A_CCR/zoca_124-07A_CCR_PET
16 ./2/zoca_124-08B_DYW/zoca_124-08B_DYW_PET
17 ./2/zoca_124-11B_JKR/zoca_124-11B_JKR_PET
18 ./2/zoca_124-15B_LAM/zoca_124-15B_LAM_PET
19 ./2/zoca_124-20B_C-T/zoca_124-20B_C-T_PET
20 ./2/zoca_124-21B_KWB/zoca_124-21B_KWB_PET
$
$ perl -lne 'chomp;
if ($ARGV eq "img.txt") {if (/.*(\d{3}[_-]\d\d[ab]).*/i) {$y=uc($1); $y=~s/-/_/; $x{$_}=$y}}
elsif (/.*zoca_(.{7}).*/i) {$y=uc($1); $y=~s/-/_/; foreach $k (keys %x) {if ($x{$k} eq $y) {$x{$k} = $_}}}
END {foreach $k (keys %x) {printf("mv %s %s\n",$k,$x{$k})}}' img.txt petdir.txt
mv ./Rearrange_zoca/fitted_data/wx322_02A_Ncere.jpg ./1/zoca_322-02A_ECB/zoca_322-02A_ECB_PET
mv ./Rearrange_zoca/fitted_data/wx226_05B_50_60.jpg ./1/zoca_226-05B_DMP/zoca_226-05B_DMP_PET
mv ./Rearrange_zoca/fitted_data/wx124_11B_50_60.jpg ./2/zoca_124-11B_JKR/zoca_124-11B_JKR_PET
mv ./Rearrange_zoca/fitted_data/wx124_02A_50_60.jpg ./2/zoca_124-02A_KJM/zoca_124-02A_KJM_PET
mv ./Rearrange_zoca/fitted_data/wx322-04A_50_60.jpg ./1/zoca_322-04A_J-K/zoca_322-04A_JK_PET
mv ./Rearrange_zoca/fitted_data/wx226_04B_50_60.jpg ./1/zoca_226-04B_JPR/zoca_226-04B_JPR_PET
mv ./Rearrange_zoca/fitted_data/wx124_21B_50_60.jpg ./2/zoca_124-21B_KWB/zoca_124-21B_KWB_PET
mv ./Rearrange_zoca/fitted_data/wx322_01A_Ncere.jpg ./1/zoca_322-01A_WSL/zoca_322-01A_WSL_PET
mv ./Rearrange_zoca/fitted_data/wx124_07A_50_60.jpg ./2/zoca_124-07A_CCR/zoca_124-07A_CCR_PET
mv ./Rearrange_zoca/fitted_data/wx322_05B_50_60.jpg ./1/zoca_322-05B-L-S/zoca_322-05B-L-S_PET
mv ./Rearrange_zoca/fitted_data/wx124_01A_50_60.jpg ./2/zoca_124-01A_KMT/zoca_124-01A_KMT_PET
mv ./Rearrange_zoca/fitted_data/wx124_08B_50_60.jpg ./2/zoca_124-08B_DYW/zoca_124-08B_DYW_PET
mv ./Rearrange_zoca/fitted_data/wx226_09A_50_60.jpg ./1/zoca_226-09A_J-L/zoca_226-09A_J-L_PET
mv ./Rearrange_zoca/fitted_data/wx322_04A_Ncere.jpg ./1/zoca_322-04A_J-K/zoca_322-04A_JK_PET
mv ./Rearrange_zoca/fitted_data/WX226_05b_Ncere.jpg ./1/zoca_226-05B_DMP/zoca_226-05B_DMP_PET
mv ./Rearrange_zoca/fitted_data/wx226_09A_Ncere.jpg ./1/zoca_226-09A_J-L/zoca_226-09A_J-L_PET
mv ./Rearrange_zoca/fitted_data/wx124_04B_50_60.jpg ./2/zoca_124-04B_DRC/zoca_124-04B_DRC_PET
mv ./Rearrange_zoca/fitted_data/wx322_01A_50_60.jpg ./1/zoca_322-01A_WSL/zoca_322-01A_WSL_PET
mv ./Rearrange_zoca/fitted_data/wrx226_12A_50_60.jpg ./1/zoca_226-12A_ATH/zoca_226-12A_ATH_PET
mv ./Rearrange_zoca/fitted_data/wx124_15B_50_60.jpg ./2/zoca_124-15B_LAM/zoca_124-15B_LAM_PET
mv ./Rearrange_zoca/fitted_data/wx226_04B_Ncere.jpg ./1/zoca_226-04B_JPR/zoca_226-04B_JPR_PET
mv ./Rearrange_zoca/fitted_data/w322_03B_60_70.jpg ./1/zoca_322-03B_KSJ/zoca_322-03B_KJ_PET
mv ./Rearrange_zoca/fitted_data/wx322_02A_50_60.jpg ./1/zoca_322-02A_ECB/zoca_322-02A_ECB_PET
mv ./Rearrange_zoca/fitted_data/wx124_20B_50_60.jpg ./2/zoca_124-20B_C-T/zoca_124-20B_C-T_PET
mv ./Rearrange_zoca/fitted_data/wx124_06A_50_60.jpg ./2/zoca_124-06A_PLD/zoca_124-06A_PLD_PET
mv ./Rearrange_zoca/fitted_data/wx124_05B_50_60.jpg ./2/zoca_124-05B_LAB/zoca_124-05B_LAB_PET
$
$
Hence, all we have to do is feed the "mv" string as an argument to Perl's system() function. The following Perl script should do the trick.
##
perl -lne 'chomp;
if ($ARGV eq "img.txt") {if (/.*(\d{3}[_-]\d\d[ab]).*/i) {$y=uc($1); $y=~s/-/_/; $x{$_}=$y}}
elsif (/.*zoca_(.{7}).*/i) {$y=uc($1); $y=~s/-/_/; foreach $k (keys %x) {if ($x{$k} eq $y) {$x{$k} = $_}}}
END {foreach $k (keys %x) {$c=sprintf("mv %s %s\n",$k,$x{$k}); system($c)}}' img.txt petdir.txt
I haven't tested it explicitly since I do not have the appropriate directory structure and files in my system.
HTH,
tyler_durden
Thank you Tyler,
could you please explain your code a little bit. its doing the right thing but i am kind of confused
This is how I did but it involved a lot of steps and few manual edits as well.
list the directory tree
ls -R >ilist.txt
find the specific directory
cat ilist.txt |grep "_PET:$" |sed 's/:$//' > petdir.txt
create the new directory where the files are supposed to be moved
awk '{print "mkdir "$0"/Fitted_Data/"}' petdir.txt |sh
awk '{print $0"/Fitted_Data/"}' petdir.txt >petfit.txt
extract the key words
awk -F"zoca_" '{print substr($2,1,7)}' petdir.txt
grep -o -i '[0-9]\{3\}[-_][0-9]\{2\}[Ab]' imgfiles.txt
sort files accordingly in excel and then move
awk '{print "mv "$3" "$1}' match_sort.txt |sh
i could have used sort -k 1,1 here
Well, it would've been much easier for me if you had asked specific questions about the code. It's very difficult for me to gauge your level of understanding of different concepts like -
(a) the options passed to the perl interpreter, e.g. "l", "n", "e"
(b) the chomp operator of Perl
(c) the concept of hashes in Perl, and in general
(d) level of understanding of regular expressions
(e) BEGIN and END blocks in a Perl one-liner
(f) the "uc" function of Perl
So, I will try to explain my code at a very high level. If you want to delve into specifics, you may want to look up all the concepts (a) through (f) in a good book on Perl, or at the perl.org website, or by talking to your local Perl guru.
In one sentence - I built a hash that had filenames from "img.txt" as the keys and their destination directories from "petdir.txt" as their corresponding values.
Here's the code spread out and documented -
1 ##
2 perl -lne 'chomp; # remove the newline at the end of the current line being processed
3 if ($ARGV eq "img.txt") { # if the current line is of file "img.txt" then
4 if (/.*(\d{3}[_-]\d\d[ab]).*/i) { # extract the "NNN[_-]NN[ab]" pattern and store it in $1
5 $y=uc($1); # change the case of $1 to upper-case and assign it to $y
6 $y=~s/-/_/; # replace the hyphen "-" by underscore "_" in $y
7 $x{$_}=$y # add (key,value) = (current_line, $y) in the hash %x
8 } # end of pattern match
9 } # end of processing "img.txt"
10 elsif (/.*zoca_(.{7}).*/i) { # at this point, we are *ALWAYS* processing "petdir.txt";
11 # so extract 7 characters after "zoca_" and store it in $1
12 $y=uc($1); # change the case of $1 to upper-case and assign it to $y
13 $y=~s/-/_/; # replace the hyphen "-" by underscore "_" in $y
14 foreach $k (keys %x) { # now we have to check if the current value of $y exists as a *key value*
15 # for any key in the hash %x; if it does, then set the *key value* to
16 # current line
17 if ($x{$k} eq $y) { # if $y is the same as a *key value* for any key in %x, then
18 $x{$k} = $_ # set that *key value* to current line (of "petdir.txt", that is)
19 } # done comparing *key value* to $y
20 } # done looping through the hash %x
21 } # done processing "petdir.txt"
22 END { # we have the complete hash now. each key is a filename from "img.txt"
23 # and its corresponding value is the destination from "petdir.txt"
24 # and this correspondence was set due to the matching regexes
25 foreach $k (keys %x) { # now just loop through the hash and display "mv <key> <value>"
26 printf("mv %s %s\n",$k,$x{$k})
27 }
28 }' img.txt petdir.txt
Hope that clears up any questions that you might have.
tyler_durden