首页 > 解决方案 > 使用 cut 和 bash

问题描述

我正在尝试切断这条线

`Cycling-Tour.Of.Great.Britain.Results-Cycling.txt`
 to
`cycling-tour.of.great.britain.txt`

所以我希望在 bash 脚本中使用 cut 删除部分 .Results-cycling ,但我没有尝试过

`txtname=$(echo "$txtname" | tr '[A-Z]' '[a-z]' | cut -d "-" -f2,11)`

它将大写字母排序,但它删除了大部分行。任何帮助将不胜感激

标签: bash

解决方案


bash有不错的字符串解析。有一些选项不会产生这么多子进程。

利用

$: declare -l txtname

首先,您不必再担心大小写了。

$: txtname=Cycling-Tour.Of.Great.Britain.Results-Cycling.txt
$: IFS=. read a b c d x e <<< "$txtname"
$: echo "$a.$b.$c.$d.$e"
cycling-tour.of.great.britain.txt

要不就

echo "${txtname/results-cycling.}"
cycling-tour.of.great.britain.txt

如果你只是想使用cut-

$: cut -d . -f 1-4,6 <<< "${txtname,,}"
cycling-tour.of.great.britain.txt

推荐阅读