
サブフォルダ名を含むすべての画像名をCSVファイルに抽出したいと思います。
このフォルダ構造があります
Desktop/Wall Arts Product Images/framed-posters/landscape/animals-and-birds/Bighorn/Bighorn.jpg
Desktop/Wall Arts Product Images/framed-posters/landscape/animals-and-birds/Lion/Lion.jpg
Desktop/Wall Arts Product Images/framed-posters/landscape/animals-and-birds/Giant-Panda/Giant-Panda.jpg
Desktop/Wall Arts Product Images/posters/landscape/Automobiles/Best-Deisgner-Jack-Daniel-Chopper/Best-Deisgner-Jack-Daniel-Chopper.jpg
Desktop/Wall Arts Product Images/posters/landscape/Automobiles/Ford-Mustang-Cars-Classic/Ford-Mustang-Cars-Classic.jpg
Desktop/Wall Arts Product Images/framed-posters/potrait/gods/Mukkunda/Mukkunda.jpg
もっとあります。
このコマンドを実行しましたが、フォルダ名ポスターとフレームポスターのみが表示されます。
'ls' | sed -e 's/^/"/' -e 's/$/"/' > files.csv
希望の出力は-->に似ています。
Image name,category,subcategory,type
Bighorn,landscape,animals and birds,framed-posters
Lion,landscape,animals and birds,framed-posters
Giant-Panda,landscape,animals and birds,framed-posters
Best-Deisgner-Jack-Daniel-Chopper,landscape,Automobiles,posters
Ford-Mustang-Cars-Classic,landscape,Automobiles,posters
Mukkunda,potrait,gods,framed-posters
CSVファイル形式で目的の出力を取得するには?
答え1
可能なので、sed
フィールドの順序を変更する方法は次のとおりです。
find -name "*.jpg" | sed -rn 's|^.||; s|[^/]*.jpg||; :a h; s|.*/(.*)|\1|p; x; s|(.*)/.*|\1| ; ta' | tr '\n' ',' | sed 's/,,/\n/g ; s/,$/\n/; s/^,//'
はい、わかりました O_O
しかし、ディレクトリ構造が一貫していない場合でも機能します。
ここにもっと読みやすくするためのコメントがあります。
find -name "*.jpg" | sed -rn '{ #get the files and pipe the output to sed
s|^.|| #remove the leading .
s|[^/]*.jpg|| #and the basename, since each image is in a directory of the same name
:a h #create a label a for this branch and put the lines into the hold space in their current state
s|.*/(.*)|\1|p #print only the last field
x #switch the hold space and pattern space
s|(.*)/.*|\1| #exclude the last field from the new pattern space, which won't do anything if there is only one field on each line
ta #if the last s command did anything, then start again from the label (:a) (thus recursively going through the fields and printing them out on separate lines in reverse order)
}' | tr '\n' ',' | sed '{ # finally turn the newlines into commas, then clean up the mess
s/,,/\n/g ; s/,$/\n/; s/^,//
}'
答え2
これを試してください:
find ~/Desktop -iname "*.jpg" -exec ls {} + | awk -F'/' ' BEGIN { OFS=", "; print "Image Name", "Category", "Subcategory", "type"} { print $(NF-1),$4, $5, $3 "" }'
イメージ名から特殊文字を削除するには、次のコードを使用します。
find ~/Desktop -iname "*.jpg" -exec rename 's/[^a-zA-Z0-9.\/-]//g' {} +
出力に合わせて調整してください。
答え3
このコマンドを試してみてください。
find . | awk -F/ '{print $(NF-1)","$(NF-3)","$(NF-2)","$(NF-4)}'
答え4
一貫したディレクトリツリー構造があると仮定すると、以下に提供されているPythonスクリプトはディレクトリツリーを巡回し、csvコンテンツをstdoutストリームに出力します(>
図のようにコマンドラインで演算子を使用してコンテンツを新しいファイルに出力します./dir_tree_csv.py > output_file.csv
)。Wall Arts Product Images
ディレクトリに配置され、そこで実行されます。
#!/usr/bin/env python
from __future__ import print_function
import os,sys
def get_all_files(treeroot):
file_list = []
for dir,subdirs,files in os.walk(treeroot):
for f in files:
if os.path.basename(__file__) in f: continue
file_list.append(os.path.join(dir,f))
return file_list
def main():
top_dir="."
if len(sys.argv) == 2: top_dir=sys.argv[1]
files = get_all_files(top_dir)
print("Image name,category,subcategory,type\n")
for f in files:
fields = f.split('/')
fields.reverse()
fields[2],fields[3] = fields[3],fields[2]
print(",".join(fields[1:-1]))
if __name__ == '__main__' : main()
テスト実行:
# Replicated directory structure with only two of the files for simplicity
$ tree
.
├── dir_tree_csv.py
├── framed-posters
│ └── landscape
│ └── animals-and-birds
│ └── Bighorn
│ └── Bighorn.jpg
└── posters
└── landscape
└── Automobiles
└── Best-Deisgner-Jack-Daniel-Chopper
└── Best-Deisgner-Jack-Daniel-Chopper.jpg
8 directories, 3 files
$ ./dir_tree_csv.py
Image name,category,subcategory,type
Best-Deisgner-Jack-Daniel-Chopper,landscape,Automobiles,posters
Bighorn,landscape,animals-and-birds,framed-posters