我想将所有带有子文件夹名称的图像名称提取到 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