Help with grouping and zipping

  
 Hi can you please help with the below ?
  
 source file:
 Column1,Column2,Column3,Column4
abc,123,dir1/FXX/F19,1
abc,123,dir1/FXX/F20,1
abc,123,dir1/FXX/F23,2
abc,123,dir1/FXX/C25,2
abc,123,dir1/FXX/X25,2
abc,123,dir1/FXX/A23,3
abc,123,dir1/FXX/Z25,3
abc,123,dir1/FXX/Y25,4

I want to move the folders (F19,F20,F23 etc. from Column 3 which is the folder path ) according to their respective grouping name in Column 4 (1,2,3,4 etc) such that folders marked under the same group move to another folder which will have the same name as that of their
group name and this folder should then get zipped.

for eg:

folders F19,F20 should move to another folder "1" (group name) and then zipped as 1.zip
folders F23,C25,X25 should move to another folder "2" (group name) and then zipped as 2.zip
folders A23,Z25,Y25 should move to another folder "3" (group name) and then zipped as 2.zip

Hello paul1234,

It is little bit not clear, so I am considering that your folders named 1 , 2 , 3 , 4 are present in path /dir1/FXX/ , if this is the case then following may help you in same.

awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A, array,",");for(j=1;j<=num;j++){print "mv ","/"array[j],"/"array[j]"/../"i};print "zip ",i".zip",i}}'   Input_file

Above will print the commands, so either you could see the output if it looks good to you, you could add | sh after above command or if you have doubt on any command try one of the command printed(hope in a non-live environment or in a test directory first), if works fine then also you could add | sh at end of the above command please. Let me know how it goes then.

Sample output will be as follows for above command too.

mv  /dir1/FXX/Y25 /dir1/FXX/Y25/../4
zip  4.zip 4
mv  /dir1/FXX/F19 /dir1/FXX/F19/../1
mv  /dir1/FXX/F20 /dir1/FXX/F20/../1
zip  1.zip 1
mv  /dir1/FXX/F23 /dir1/FXX/F23/../2
mv  /dir1/FXX/C25 /dir1/FXX/C25/../2
mv  /dir1/FXX/X25 /dir1/FXX/X25/../2
zip  2.zip 2
mv  /dir1/FXX/A23 /dir1/FXX/A23/../3
mv  /dir1/FXX/Z25 /dir1/FXX/Z25/../3
zip  3.zip 3

EDIT: Or try following one too, if you want to have complete path for zipping folders also.

awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A, array,",");for(j=1;j<=num;j++){print "mv ","/"array[j],"/"array[j]"/../"i};print "zip ",i".zip","/"array[num]"/../"}}'  Input_file

PS: Not tested this command, so only above explanation.

Thanks,
R. Singh

Hi Ravinder ,
Just to clarify 1,2,3,4 are the new folders to be created on the fly in a totally different path say /dirM/XX according to the grouping names in(Column 4)
In the above example:

folders F19,F20 should move to new folder "1" as per their group name in column 4 (new folder name should be same as group name) and then zipped as 1.zip
folders F23,C25,X25 should move to new folder "2" as per their group name in column 4 (new folder name should be same as group name) and then zipped as 2.zip
folders A23,Z25,Y25 should move to new folder "3" as per their group name in column 4 (new folder name should be same as group name) and then zipped as 3.zip

Hello paul1234,

Could you please try following and let me know if this helps you.
Also don't get confuse with ../4 etc in sample output it means it will go one folder level up and create that directory, to make code simple only I have kept it like that.

awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A, array,",");for(j=1;j<=num;j++){VAL=VAL?VAL ORS "mv " OFS "/"array[j] OFS"/"array[j]"/../"i:"mv " OFS "/"array[j] OFS "/"array[j]"/../"i};print "mkdir /"array[num]"/../"i"; " RS VAL RS "zip ",i".zip","/"array[num]"/../"i}}'   Input_file

It will print the output like as follows.

mkdir /dir1/FXX/Y25/../4;
mv  /dir1/FXX/Y25 /dir1/FXX/Y25/../4
zip  4.zip /dir1/FXX/Y25/../4
mkdir /dir1/FXX/F20/../1;
mv  /dir1/FXX/Y25 /dir1/FXX/Y25/../4
mv  /dir1/FXX/F19 /dir1/FXX/F19/../1
mv  /dir1/FXX/F20 /dir1/FXX/F20/../1
zip  1.zip /dir1/FXX/F20/../1
mkdir /dir1/FXX/X25/../2;
mv  /dir1/FXX/Y25 /dir1/FXX/Y25/../4
mv  /dir1/FXX/F19 /dir1/FXX/F19/../1
mv  /dir1/FXX/F20 /dir1/FXX/F20/../1
mv  /dir1/FXX/F23 /dir1/FXX/F23/../2
mv  /dir1/FXX/C25 /dir1/FXX/C25/../2
mv  /dir1/FXX/X25 /dir1/FXX/X25/../2
zip  2.zip /dir1/FXX/X25/../2
mkdir /dir1/FXX/Z25/../3;
mv  /dir1/FXX/Y25 /dir1/FXX/Y25/../4
mv  /dir1/FXX/F19 /dir1/FXX/F19/../1
mv  /dir1/FXX/F20 /dir1/FXX/F20/../1
mv  /dir1/FXX/F23 /dir1/FXX/F23/../2
mv  /dir1/FXX/C25 /dir1/FXX/C25/../2
mv  /dir1/FXX/X25 /dir1/FXX/X25/../2
mv  /dir1/FXX/A23 /dir1/FXX/A23/../3
mv  /dir1/FXX/Z25 /dir1/FXX/Z25/../3
zip  3.zip /dir1/FXX/Z25/../3

If happy with above print commands, do the same as mentioned in my previous post pipe's above command's output into | sh .
EDIT: Adding a non-one liner form of solution successfully too now.

awk -F',' 'NR>1{
                A[$4]=A[$4]?A[$4]","$3:$3
               }
           END {
                for(i in A){
                                num=split(A, array,",");
                                for(j=1;j<=num;j++){
                                                        VAL=VAL?VAL ORS "mv " OFS "/"array[j] OFS"/"array[j]"/../"i:"mv " OFS "/"array[j] OFS "/"array[j]"/../"i
                                                   };
                                print "mkdir /"array[num]"/../"i"; " RS VAL RS "zip ",i".zip","/"array[num]"/../"i
                           }
               }
           '    Input_file
 

Thanks,
R. Singh