2

I have a folder structure like :-

                                            Type1                                           Type 2      
                                                |                                               |
                    ----------------------------------------------------------------------------------------
                    |                                       |                                   |                       
                subType1                                 subType2                           subType1             
                    |                                       |                                   |               
        ----------------------------------------------------------------------------------------------------------
        |                   |                   |                   |                 |                   |     
        typeA               typeB             typeA               typeB             typeA               typeB   
    -------------------------------------------------------------------------------------------------------------
    |   |   |           |    |    |         |   |   |           |   |   |       |   |   |            |    |     |
    F1  F2  F3          F1   F2   F3        F1  F2  F3          F1  F2  F3      F1  F2  F3           F1   F2    F3
            |                     |                                     |                                       |
            |                     |                                     |                                       |
            \/                    \/                                    \/                                      \/
    --------------------------------------------------------------------------------------------------------------
    |       |      |         |       |                      |         |      |                          |       |
    black  red   blue       black   red                     black     red    blue                       black  red

In each folder of color some images are there like this :-(and many more )

    Type1-subType1-typeA-black-dfsdf1.JPG       Type1-subType1-typeB-black-one.JPG
    Type1-subType1-typeA-red-dfsdf1.JPG         Type1-subType1-typeB-red-one.JPG
    Type1-subType1-typeA-blue-dfsdf1.JPG        Type1-subType1-typeB-black-oh.JPG
    Type1-subType1-typeA-black-2dsfsdf.JPG      Type1-subType1-typeB-red-oh.JPG
    Type1-subType1-typeA-red-2dsfsdf.JPG
    Type1-subType1-typeA-blue-2dsfsdf.JPG
    Type1-subType1-typeA-black-3asdfdf3.JPG
    Type1-subType1-typeA-red-3asdfdf3.JPG
    Type1-subType1-typeA-blue-3asdfdf3.JPG

    Type1-subType2-typeB-black-SSS.JPG      Type2-subType1-typeB-black-5.JPG
    Type1-subType2-typeB-red-SSS.JPG        Type2-subType1-typeB-black-5.JPG
    Type1-subType2-typeB-blue-SSS.JPG       Type2-subType1-typeB-black-565.JPG
    Type1-subType2-typeB-black-AAA.JPG      Type2-subType1-typeB-black-565.JPG
    Type1-subType2-typeB-red-AAA.JPG        
    Type1-subType2-typeB-blue-AAA.JPG   

I want to read each images and save into individual csv file. So the csv file format will be:
For folder structure Type1-subType1-typeA -dfsdf1.JPG into file1.csv

          Image name                        |colour | subcat| subtype  | Type1 |
    Type1-subType1-typeA-black-dfsdf1.JPG   | black | typeA | subType1 | Type1 | 
    Type1-subType1-typeA-red-dfsdf1.JPG     | red   | typeA | subType1 | Type1 | 
    Type1-subType1-typeA-blue-dfsdf1.JPG    | blue  | typeA | subType1 | Type1 | 

For folder structure Type1-subType1-typeA -2dsfsdf.jpg file2.csv

          Image name                    |colour | subcat| subtype  | Type1 |
Type1-subType1-typeA-black-2dsfsdf.JPG  | black | typeA | subType1 | Type1 | 
Type1-subType1-typeA-red-2dsfsdf.JPG    | red   | typeA | subType1 | Type1 | 
Type1-subType1-typeA-blue-2dsfsdf.JPG   | blue  | typeA | subType1 | Type1 

And So on for other folders also.How can i get the desired out put?

Note: The image name reflects the folder structure.

So far, I've read all images from different folder into a single csv file. Here is my output:

            Image name                  |colour | subcat| subtype  | Type1 |
Type1-subType1-typeA-black-dfsdf1.JPG   | black | typeA | subType1 | Type1 | 
Type1-subType1-typeA-red-dfsdf1.JPG     | red   | typeA | subType1 | Type1 | 
Type1-subType1-typeA-blue-dfsdf1.JPG    | blue  | typeA | subType1 | Type1 | 
Type1-subType1-typeA-black-2dsfsdf.JPG  | black | typeA | subType1 | Type1 | 
Type1-subType1-typeA-red-2dsfsdf.JPG    | red   | typeA | subType1 | Type1 | 
Type1-subType1-typeA-blue-2dsfsdf.JPG   | blue  | typeA | subType1 | Type1 | 
Type1-subType1-typeA-black-3asdfdf3.JPG | black | typeA | subType1 | Type1 | 
Type1-subType1-typeA-red-3asdfdf3.JPG   | red   | typeA | subType1 | Type1 | 
Type1-subType1-typeA-blue-3asdfdf3.JPG  | blue  | typeA | subType1 | Type1 | 
Type1-subType2-typeB-black-SSS.JPG      | black | typeB | subType2 | Type1 | 
Type1-subType2-typeB-red-SSS.JPG        | red   | typeB | subType2 | Type1 | 
Type1-subType2-typeB-blue-SSS.JPG       | blue  | typeB | subType2 | Type1 | 
Type1-subType2-typeB-black-AAA.JPG      | black | typeB | subType2 | Type1 | 
Type1-subType2-typeB-red-AAA.JPG        | red   | typeB | subType2 | Type1 | 
Type1-subType2-typeB-blue-AAA.JPG       | blue  | typeB | subType2 | Type1 | 

Please suggest how can I achieve my desired output.

  • 1
    Wow. Kudos for effort in making that ASCII structure map. Next time though, you might want to think about just using tree instead. – terdon Apr 12 '17 at 12:41
  • 1
    Actually. Please do use tree, your ASCII is hard to follow. What are F1, F2 etc? If directories, why don't they appear in your csv? Are the image names always the same? – terdon Apr 12 '17 at 12:47
  • @terdon F1,F2 also directories.All Images in that directories, so that path is constant.In image name it(directories name) will not be there.Image name will be same but color,sub-folder,type will be different .So image 1 name should be in one csv file, image 1 name should be in second csv file, and so on – Urvashi Apr 12 '17 at 12:58
  • How to use tree for folder structure can you please explain – Urvashi Apr 12 '17 at 12:59
  • Just run the command tree from the parent directory of Type1 and Type2. It will print the directory structure for you. – terdon Apr 12 '17 at 13:00
0

as a rule, you shouldn't parse output of ls, however assuming you have no 'funny' names (like space or line break).

I would use awk to parse file name

ls | awk -F- '{printf "%s,%s,%s,%s,%s,\n",$0,$4,$3,$2,$1}' > file.csv

where

  • -F- tell awk to use - as separator
  • $0 whole line
  • $4..$1 -th field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.