【发布时间】:2017-09-29 05:56:56
【问题描述】:
脚本应该列出当前目录下除后缀的文件(并以不带后缀的形式结束列表)。
示例(也称为“.extension:”)
.c: first.c main.c var.c
.h: const.h first.h
.odt: relazione.odt
makefile README COPYING
我正在尝试使用 ls、sort、uniq,但我做不到。 有人可以帮助我吗?
我用python写了一个解决方案:
#!/usr/bin/env python3
import os
dictionary={}
for dirpath,_,files in os.walk("./"):
for f in files:
path = os.path.abspath(os.path.join(dirpath, f))
_ , ext = os.path.splitext(path)
if ext not in dictionary:
dictionary[ext] = []
dictionary[ext].append(f)
for k in dictionary:
if k != "":
print(k, end=": ")
for i in dictionary.get(k):
print(i, end= " ")
print("")
for i in dictionary.get(""):
print(i, end= " ")
print("")
【问题讨论】:
-
你能告诉我们你的尝试吗?
-
我试过这个以获得后缀 ls |排序-t。 | awk -F '.' '{if (NF>1) {print $NF}}'
标签: bash shell awk command-line