bash 如何在shell脚本中将部分变量提取到另一个变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9901199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:53:24  来源:igfitidea点击:

How to extract part of variable into another variable in shell script

bashshellsh

提问by Alexey Kamenskiy

I have a variable which looks like xx-xx-xx-xx where each xx is a number (length of each xx is unknown)

我有一个看起来像 xx-xx-xx-xx 的变量,其中每个 xx 是一个数字(每个 xx 的长度未知)

I need to extract those numbers in separate variables to be able manipulate them. I tried to look at regular expressions but couldnt see any solution (or i am just blind enough not to notice.

我需要在单独的变量中提取这些数字才能操纵它们。我试图查看正则表达式,但看不到任何解决方案(或者我只是盲目地没有注意到。

Ideally solution should look like

理想的解决方案应该看起来像

#!/bin/sh
# assume VARIABLE equals 1234-123-456-890
VARIABLE=

# HERE SOME CODE assigning variables $PART1 $PART2 $PART3 $PART4

echo $PART1-$PART2-$PART3-$PART4
# Output will give us back 1234-123-456-890

I am quite new to shell scripting so i might have missed something.

我对 shell 脚本很陌生,所以我可能错过了一些东西。

回答by FatalError

Using bashyou could use an array like this:

使用bash你可以使用这样的数组:

#!/bin/bash
VARIABLE=1234-123-456-890

PART=(${VARIABLE//-/ })

echo ${PART[0]}-${PART[1]}-${PART[2]}-${PART[3]}

The ${VARIABLE//-/ }expansion changes all - to spaces and then it's split on word boundaries into an array.

${VARIABLE//-/ }扩张改变了这一切-为空格,然后是单词边界分割成一个数组。

Alternatively, you could use read:

或者,您可以使用read

#!/bin/bash
VARIABLE=1234-123-456-890

read PART1 PART2 PART3 PART4 <<< "${VARIABLE//-/ }"
echo $PART1-$PART2-$PART3-$PART4

To make it work in sh, you could change it slightly and set IFS, the input field separator:

要使其在 中工作sh,您可以稍微更改它并设置IFS输入字段分隔符:

#!/bin/sh
VARIABLE=1234-123-456-890

old_ifs="$IFS"
IFS=-
read PART1 PART2 PART3 PART4 <<EOF
$VARIABLE
EOF

IFS="$old_ifs"
echo $PART1-$PART2-$PART3-$PART4

Caveat: this was only tested with bashrunning in shmode.

警告:这仅bashsh模式下运行时进行了测试。

回答by Rony

/bin/shis dash(see section "Parameter Expansion")

/bin/sh破折号(参见“参数扩展”部分)

#!/bin/sh
# assume VARIABLE equals 1234-123-456-890

VARIABLE=""

PART4="$VARIABLE"       # PART4 is 1234-123-456-890

PART1="${PART4%%-*}"    # PART1 is 1234
PART4="${PART4#*-}"     # PART4 is 123-456-890

PART2="${PART4%%-*}"    # PART2 is 123
PART4="${PART4#*-}"     # PART4 is 456-890

PART3="${PART4%%-*}"    # PART3 is 456
                        # PART4 is 456-890

PART4="${PART4#*-}"     # PART4 is 890

echo "$PART1-$PART2-$PART3-$PART4"

Test for $PART1-$PART2-$PART3-$PART4

测试 $PART1-$PART2-$PART3-$PART4

$ ./test.sh 1234-123-456-890
1234-123-456-890

$ ./test.sh 1234-abc-789-ABCD
1234-abc-789-ABCD

回答by potong

This might work for you:

这可能对你有用:

a="1234-123-456-890"
OIFS=$IFS; IFS=-; b=($a); echo "${b[*]}"; echo "${b[@]}"; IFS=$OIFS; echo "${b[*]}"
1234-123-456-890
1234 123 456 890
1234 123 456 890
echo "${b[0]}"
1234
echo "${b[1]}"
123
echo "${b[2]}"
456
echo "${b[3]}"
890

回答by peon

PART1=`echo $VARIABLE | cut -d'-' -f1`

回答by Balaswamy Vaddeman

 echo "abc-def-ghi"|cut -f1 -d'-'

more about cut

更多关于切割