简体   繁体   English

拆分字符串以打印前两个由“ - ”分隔的字符。在Bash中

[英]Split a string to print first two characters delimited by “-” In Bash

I am listing the AWS region names. 我列出了AWS区域名称。

us-east-1
ap-southeast-1

I want to split the string to print specific first characters delimited by - ie 'two characters'-'one character'-'one character'. 我想拆分字符串以打印特定的第一个字符-即“两个字符” - “一个字符” - “一个字符”。 So us-east-1 should be printed as use1 and ap-southeast-1 should be printed as aps1 所以us-east-1应打印为use1ap-southeast-1应打印为aps1

I have tried this and it's giving me expected results. 我试过这个,它给了我预期的结果。 I was thinking if there is a shorter way to achieve this. 我在想是否有更短的方法来实现这一目标。

region=us-east-1 
regionlen=$(echo -n $region | wc -m) 
echo $region | sed 's/-//' | cut -c 1-3,expr $regionlen - 2-expr $regionlen - 1 

How about using sed : 使用sed怎么样:

echo "$region" | sed -E 's/^(.[^-]?)[^-]*-(.)[^-]*-(.).*$/\1\2\3/'

Explanation: the s/pattern/replacement/ command picks out the relevant parts of the region name, replacing the entire name with just the relevant bits. 说明: s/pattern/replacement/命令选取区域名称的相关部分,仅用相关位替换整个名称。 The pattern is: 模式是:

^         - 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

The bits in parentheses get captured, so \\1\\2\\3 pulls them out and replaces the whole thing with just those. 括号中的位被捕获,因此\\1\\2\\3将它们拉出来并用这些替换整个事物。

IFS influencing field splitting step of parameter expansion: IFS影响参数扩展的字段分割步骤:

$ str=us-east-2
$ IFS=- eval 'set -- $str'
$ echo $#
3
$ echo $1
us
$ echo $2
east
$ echo $3

No external utilities; 没有外部工具; just processing in the language. 只是用语言处理。

This is how smartly written build configuration scripts parse version numbers like 1.13.4 and architecture strings like i386-gnu-linux . 这就是1.13.4编写的构建配置脚本如何解析版本号,如1.13.4和架构字符串,如i386-gnu-linux

The eval can be avoided, if we save and restore IFS . 如果我们保存并恢复IFS ,则可以避免eval

$ save_ifs=$IFS; set -- $str; IFS=$save_ifs

Using bash, and assuming that you need to distinguish between things like southwest and southeast: 使用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"

How about: 怎么样:

region="us-east-1"
echo "$region" | (IFS=- read -r a b c; echo "$a${b:0:1}${c:0:1}")
use1

A simple sed - 一个简单的sed -

$: printf "us-east-1\nap-southeast-1\n" |
     sed -E 's/-(.)[^-]*/\1/g'

To keep noncardinal specifications like southeast distinct from south at the cost of adding an optional additional character - 为了保持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;'

If you could have south-southwest , add g to those directional reductions. 如果你可以在south-southwest ,那么将g添加到那些方向减少。

if you MUST have exactly 4 characters of output, I recommend mapping the eight or 16 map directions to specific characters, so that north is N, northeast is maybe O and northwest M... that sort of thing. 如果你必须有4个字符的输出,我建议将8或16个地图方向映射到特定字符,这样北方是N,东北方向可能是O和西北M ......那种东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM