Help with grouping and zipping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grouping and zipping
# 1  
Old 07-05-2017
Help with grouping and zipping

Code:
  
 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:
Code:
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

Moderator's Comments:
Mod Comment When using code tags, take care what you put them around. Parts of your descriptive text was included as well.

Last edited by zaxxon; 07-06-2017 at 04:10 AM.. Reason: code tags
# 2  
Old 07-05-2017
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.
Code:
awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A[i], 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.
Code:
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.
Code:
awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A[i], 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

Last edited by RavinderSingh13; 07-05-2017 at 03:46 PM..
# 3  
Old 07-05-2017
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:
Code:
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


Last edited by zaxxon; 07-06-2017 at 04:11 AM.. Reason: code tags
# 4  
Old 07-05-2017
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.
Code:
awk -F',' 'NR>1{A[$4]=A[$4]?A[$4]","$3:$3} END{for(i in A){num=split(A[i], 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.
Code:
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.
Code:
awk -F',' 'NR>1{
                A[$4]=A[$4]?A[$4]","$3:$3
               }
           END {
                for(i in A){
                                num=split(A[i], 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

Last edited by RavinderSingh13; 07-05-2017 at 04:14 PM.. Reason: Adding a non-one liner form of solution successfully too now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Zipping

Good Morning, I'd like to archive an old user's files in the home directory on Solaris 9 Will this work? cd home tar -zcvf jsmitharchive.tar.gz jsmith/ ---------- Post updated at 09:37 AM ---------- Previous update was at 09:33 AM ---------- Also- is the last /necessary (after... (4 Replies)
Discussion started by: Stellaman1977
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Help with Grouping and zipping folders

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... (1 Reply)
Discussion started by: paul1234
1 Replies

3. Shell Programming and Scripting

Zipping without extension

I currently have a code that find and zip all files in current folder and zip it, the problem is the name of the zip will include the extension as well and I don't want it. for ex: Volvo-red.swf -> Volvo-red.swf.zip find . -maxdepth 1 -type f ! -name ".*" -exec bash -c 'zip -r "$0.zip"... (6 Replies)
Discussion started by: Frozen77
6 Replies

4. Shell Programming and Scripting

Zipping files

Hi Guys, The script below works but it creates a zip folder under 123_arch.zip -- test1 -- orig1.txt -- orig2.txt -- orig3.txt -- orig4.txt I don't want the sub directory test1 but everything under the base *arch name I can not create a long name with... (4 Replies)
Discussion started by: GaryP1973
4 Replies

5. UNIX for Dummies Questions & Answers

Zipping files

Hi All, I have a scenario where in am using uuencode to send a txt file as an excel to end users( email attachment).I have 7 different files and these files are sent as emails 7 times... So my question is, can i not zip all the 7 files at once and attach those files in a single... (9 Replies)
Discussion started by: saggiboy10
9 Replies

6. UNIX for Dummies Questions & Answers

Help With zipping a file

Hi I need to zip a file and move it into another folder along with the timestamp. The orginal file must be removed from the source directory Source : folder1/source12.txt folder2 After zipping Folder1/Folder2/source12.zip Any help will be greatly appreciarted ... (5 Replies)
Discussion started by: akshay01987
5 Replies

7. Shell Programming and Scripting

Zipping and moving

Hi, I have got the script which find out the file that was modified 3 days ago now i want that all the files which fall into this category to be get zipped and then send to another directory...plz can u suggest me for that or any commands (18 Replies)
Discussion started by: SARAL SAXENA
18 Replies

8. UNIX for Dummies Questions & Answers

Zipping files?

how would i zip a file? what does zip mean? (4 Replies)
Discussion started by: trob
4 Replies

9. Shell Programming and Scripting

zipping files

Dear Experts, I need a script which will zipped the files older than 2 days. but i dont want to use find . * -mtime 2. Is there is any other method to achive this task. i will ececute the script daily. Regards, (3 Replies)
Discussion started by: shary
3 Replies

10. UNIX for Dummies Questions & Answers

Zipping

Hi In unix i want to zip the files in a directory excluding *.dmp, *.log, *.lst, *.out files in that directory. pls let me know what command to use. $zip ........ ? Thanks (1 Reply)
Discussion started by: dreams5617
1 Replies
Login or Register to Ask a Question