Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中像这样甩线的简单方法?_Bash_Shell - Fatal编程技术网

在bash中像这样甩线的简单方法?

在bash中像这样甩线的简单方法?,bash,shell,Bash,Shell,现在我有了“temp:10”格式的字符串,并且我使用temp=$(echo$str | awk'{split($0,array,“:”)}结束{print array[1]}')来进行拆分,这是一种过于复杂和缓慢的方式。必须有一种更简单的方法来完成此操作?如果我理解正确,在本例中,您需要:,temp之前的值。如果是,则可以使用cut命令: cut -d':' -f1 $ str="temp:10" $ IFS=: read before after <<< "$str" $

现在我有了
“temp:10”
格式的字符串,并且我使用
temp=$(echo$str | awk'{split($0,array,“:”)}结束{print array[1]}')
来进行拆分,这是一种过于复杂和缓慢的方式。必须有一种更简单的方法来完成此操作?

如果我理解正确,在本例中,您需要
temp
之前的值。如果是,则可以使用
cut
命令:

cut -d':' -f1
$ str="temp:10"
$ IFS=: read before after <<< "$str"
$ echo "$before"
temp
$ echo "$after"
10

如果我没弄错的话,在本例中,您需要
temp
前面的值。如果是,则可以使用
cut
命令:

cut -d':' -f1
$ str="temp:10"
$ IFS=: read before after <<< "$str"
$ echo "$before"
temp
$ echo "$after"
10

使用bash的参数扩展并删除后缀:

temp=${str%%:*}

使用bash的参数扩展并删除后缀:

temp=${str%%:*}

还有
read
命令:

cut -d':' -f1
$ str="temp:10"
$ IFS=: read before after <<< "$str"
$ echo "$before"
temp
$ echo "$after"
10
$str=“温度:10”

$IFS=:在之前读取,还有
read
命令:

cut -d':' -f1
$ str="temp:10"
$ IFS=: read before after <<< "$str"
$ echo "$before"
temp
$ echo "$after"
10
$str=“温度:10”

$IFS=:tom在答案之前和之后读取是最理想的,但还有一点:
awk'{split($0,array,“:”)}END{print array[1]}')
本身就有些过分了。您可以使用
awk-F:'{print$1}'
来实现同样的效果。tom的答案是最理想的,但还有一点:
awk'{split($0,array,“:”)}END{print array[1]}')
本身就有些过分了。您可以使用
awk-F:'{print$1}'
实现同样的功能。