从字符串中删除字符

从字符串中删除字符

有谁知道如何从 bash 中的以下字符串中删除数字和句点?

3104.302279 3104.302329 3104.302829 3104.302829 3104.303206 3104.303130 3104.303175 3104.303398 3104.303315 3104.303420 3104.303485 3104.303479 3104.303513 3104.303518 3104.302973 3104.303185 3104.303349 3104.303398 3104.303518 3104.303518 3104.303503 3104.303519 3104.303519 3104.303130 3104.303521 3104.303521 3104.303521 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303523 3104.303185 3104.303185 3104.303175 3104.303185 3104.303523 3104.303522 3104.303185 3104.303185 3104.303420 3104.303185 3104.303185 3104.303185 3104.303526 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303527 3104.303525 3104.303525 3104.303479 3104.303315 3104.303315 3104.303315 3104.303315 3104.303315

保留句号后面的数字不变。我还想将结果存储在变量而不是文件中。

答案1

在bash中,您可以将空格分隔的值读取到数组中,然后使用删除前导字符的参数替换来扩展数组的元素:

read -a vals <<< '3104.302279 3104.302329 3104.302829 3104.302829 3104.303206 3104.303130 3104.303175 3104.303398 3104.303315 3104.303420 3104.303485 3104.303479 3104.303513 3104.303518 3104.302973 3104.303185 3104.303349 3104.303398 3104.303518 3104.303518 3104.303503 3104.303519 3104.303519 3104.303130 3104.303521 3104.303521 3104.303521 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303523 3104.303185 3104.303185 3104.303175 3104.303185 3104.303523 3104.303522 3104.303185 3104.303185 3104.303420 3104.303185 3104.303185 3104.303185 3104.303526 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303527 3104.303525 3104.303525 3104.303479 3104.303315 3104.303315 3104.303315 3104.303315 3104.303315'

var="${vals[@]#*.}"

echo "$var"
302279 302329 302829 302829 303206 303130 303175 303398 303315 303420 303485 303479 303513 303518 302973 303185 303349 303398 303518 303518 303503 303519 303519 303130 303521 303521 303521 303185 303185 303185 303185 303185 303185 303185 303185 303185 303185 303523 303185 303185 303175 303185 303523 303522 303185 303185 303420 303185 303185 303185 303526 303185 303185 303185 303185 303185 303527 303525 303525 303479 303315 303315 303315 303315 303315

答案2

MYVAR=`sed -E 's/[0-9]+.([0-9]+)/\1/g' stringfile`

答案3

如果 var a 是整个数字列表:

$ a='3104.302279 3104.302329 3104.302829 3104.302829 3104.303206 3104.303130 3104.303175 3104.303398 3104.303315 3104.303420 3104.303485 3104.303479 3104.303513 3104.303518 3104.302973 3104.303185 3104.303349 3104.303398 3104.303518 3104.303518 3104.303503 3104.303519 3104.303519 3104.303130 3104.303521 3104.303521 3104.303521 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303523 3104.303185 3104.303185 3104.303175 3104.303185 3104.303523 3104.303522 3104.303185 3104.303185 3104.303420 3104.303185 3104.303185 3104.303185 3104.303526 3104.303185 3104.303185 3104.303185 3104.303185 3104.303185 3104.303527 3104.303525 3104.303525 3104.303479 3104.303315 3104.303315 3104.303315 3104.303315 3104.303315'

然后我们可以将 b 变成 a 中的数字数组:

$ b=( $a )

并打印所有不带前导整数的 b 元素(点之前):

$ printf '%s ' "${b[@]#*.}"
302279 302329 302829 302829 303206 303130 303175 303398 303315 303420 303485 303479 303513 303518 302973 303185 303349 303398 303518 303518 303503 303519 303519 303130 303521 303521 303521 303185 303185 303185 303185 303185 303185 303185 303185 303185 303185 303523 303185 303185 303175 303185 303523 303522 303185 303185 303420 303185 303185 303185 303526 303185 303185 303185 303185 303185 303527 303525 303525 303479 303315 303315 303315 303315 303315

并将其分配给其他变量 c (与 bash 一样,我将使用 printf -v):

$ printf -v c '%s ' "${b[@]#*.}"

就是这样,$c 将包含列表(空格分隔)。

答案4

假设字符串位于单个变量中$a,并且要删除的前缀字符串是“3104.“,(它在OP中),那么这个bash 模式替换作品:

b="${a//3104.}" ; echo $b

相关内容