【问题标题】:script to split file name into part1 and part2 and create directory part1 and put file part2 into part1 directory将文件名拆分为 part1 和 part2 并创建目录 part1 并将文件 part2 放入 part1 目录的脚本
【发布时间】:2016-12-20 16:53:59
【问题描述】:

我有一堆格式为 st_hwk.txt 的文件,如果你必须知道,这就是 Moodle 下载作业进行评分的方式。它采用 hwk 的名称并预先添加用户名。 该解决方案需要在我正在研究的 Linux bc 上运行。 前任: 我下载了j smith_hwk1a.txt、j smith_hwk1b.txt、m wong_hwk1a.txt、m wong_hwk1b.txt。 (是的,文件名有 fname space lname)

它应该读取文件名并创建目录 jsmith 和 mwong。 (没有空间) 放入 jsmith 文件 hwk1a.txt 和 hwk1b.txt。 (来自 jsmith 的 hwk1) 放入mwong文件hwk1a.txt和hwk1b.txt。 (来自 mwong 的 hwk1)。 您可以在典型的 linux、bash、php、...上使用任何工具? 谢谢

【问题讨论】:

  • 请添加您尝试过的代码

标签: python linux bash split directory


【解决方案1】:
for f in *_hwk*.txt; do
  n=$(echo "$f"|tr -d ' '|tr _ /);   # delete spaces, convert _ to /
  mkdir -p "$(dirname "$n")";        # make directory if needed
  mv "$f" "$n";                      # move the file
done

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多