[英]Split a string to print first two characters delimited by “-” In Bash
我列出了AWS区域名称。
us-east-1
ap-southeast-1
我想拆分字符串以打印特定的第一个字符-
即“两个字符” - “一个字符” - “一个字符”。 所以us-east-1
应打印为use1
, ap-southeast-1
应打印为aps1
我试过这个,它给了我预期的结果。 我在想是否有更短的方法来实现这一目标。
region=us-east-1
regionlen=$(echo -n $region | wc -m)
echo $region | sed 's/-//' | cut -c 1-3,expr $regionlen - 2-expr $regionlen - 1
使用sed
怎么样:
echo "$region" | sed -E 's/^(.[^-]?)[^-]*-(.)[^-]*-(.).*$/\1\2\3/'
说明: s/pattern/replacement/
命令选取区域名称的相关部分,仅用相关位替换整个名称。 模式是:
^ - the beginning of the string
(.[^-]?) - the first character, and another (if it's not a dash)
[^-]* - any more things up to a dash
- - a dash (the first one)
(.) - The first character of the second word
[^-]*- - the rest of the second word, then the dash
(.) - The first character of the third word
.*$ - Anything remaining through the end
括号中的位被捕获,因此\\1\\2\\3
将它们拉出来并用这些替换整个事物。
IFS
影响参数扩展的字段分割步骤:
$ str=us-east-2
$ IFS=- eval 'set -- $str'
$ echo $#
3
$ echo $1
us
$ echo $2
east
$ echo $3
没有外部工具; 只是用语言处理。
这就是1.13.4
编写的构建配置脚本如何解析版本号,如1.13.4
和架构字符串,如i386-gnu-linux
。
如果我们保存并恢复IFS
,则可以避免eval
。
$ save_ifs=$IFS; set -- $str; IFS=$save_ifs
使用bash,并假设您需要区分西南和东南等事物:
s=ap-southwest-1
a=${s:0:2}
b=${s#*-}
b=${b%-*}
c=${s##*-}
bb=
case "$b" in
south*) bb+=s ;;&
north*) bb+=n ;;&
*east*) bb+=e ;;
*west*) bb+=w ;;
esac
echo "$a$bb$c"
怎么样:
region="us-east-1"
echo "$region" | (IFS=- read -r a b c; echo "$a${b:0:1}${c:0:1}")
use1
一个简单的sed
-
$: printf "us-east-1\nap-southeast-1\n" |
sed -E 's/-(.)[^-]*/\1/g'
为了保持noncardinal规范,比如southeast
不同于south
在添加一个可选的附加字符的成本-
$: printf "us-east-1\nap-southeast-1\n" |
sed -E '
s/north/n/;
s/south/s/;
s/east/e/;
s/west/w/;
s/-//g;'
如果你可以在south-southwest
,那么将g
添加到那些方向减少。
如果你必须有4个字符的输出,我建议将8或16个地图方向映射到特定字符,这样北方是N,东北方向可能是O和西北M ......那种东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.