【问题标题】:How to display 0-nth character in a file name如何在文件名中显示第 0 个字符
【发布时间】:2015-02-09 10:40:54
【问题描述】:

我有一个文件 = 'test_acn_mark_down_201400000.csv'。 我想在 unix 中只有一个值 file1='test_acn_mark_down' 这意味着从位置 0 到 f4,分隔符将是 '-'。

请帮帮我。

【问题讨论】:

  • 输出示例中的分隔符不是'-'。这是故意的吗?

标签: shell unix ksh


【解决方案1】:

你可以使用cut:

file='test_acn_mark_down_201400000.csv'
echo "$file" | cut -d _ -f1-4

file1=$(echo "echi $file" | cut -d _ -f1-4)
echo "$file1"
test_acn_mark_down

【讨论】:

    【解决方案2】:

    您要求提供前四个字段。 最好的方法是使用 cut:

    file=test_acn_mark_down_201400000.csv
    file1=$(echo "${file}" | cut -d _ -f1-4)
    

    当您知道文件名的其余部分没有“_”字符时,您还可以使用特殊语法删除末尾从最后一个下划线开始的所有内容:

    file=test_acn_mark_down_201400000.csv
    file1="${file%_*}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 2019-11-12
      • 2014-03-01
      相关资源
      最近更新 更多