Sponsored Content
Top Forums Shell Programming and Scripting Help with grouping and zipping Post 303000172 by RavinderSingh13 on Wednesday 5th of July 2017 03:06:45 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy