【问题标题】:Accessing last x characters of a string in Bash在 Bash 中访问字符串的最后 x 个字符
【发布时间】:2013-11-20 10:27:48
【问题描述】:

我发现${string:0:3} 可以访问字符串的前 3 个字符。是否有一种同样简单的方法可以访问最后三个字符?

【问题讨论】:

    标签: string bash extract


    【解决方案1】:

    string的最后三个字符:

    ${string: -3}
    

    ${string:(-3)}
    

    (注意第一种形式中:-3 之间的空格)。

    请参考Shell Parameter Expansion in the reference manual

    ${parameter:offset}
    ${parameter:offset:length}
    
    Expands to up to length characters of parameter starting at the character
    specified by offset. If length is omitted, expands to the substring of parameter
    starting at the character specified by offset. length and offset are arithmetic
    expressions (see Shell Arithmetic). This is referred to as Substring Expansion.
    
    If offset evaluates to a number less than zero, the value is used as an offset
    from the end of the value of parameter. If length evaluates to a number less than
    zero, and parameter is not ‘@’ and not an indexed or associative array, it is
    interpreted as an offset from the end of the value of parameter rather than a
    number of characters, and the expansion is the characters between the two
    offsets. If parameter is ‘@’, the result is length positional parameters
    beginning at offset. If parameter is an indexed array name subscripted by ‘@’ or
    ‘*’, the result is the length members of the array beginning with
    ${parameter[offset]}. A negative offset is taken relative to one greater than the
    maximum index of the specified array. Substring expansion applied to an
    associative array produces undefined results.
    
    Note that a negative offset must be separated from the colon by at least one
    space to avoid being confused with the ‘:-’ expansion. Substring indexing is
    zero-based unless the positional parameters are used, in which case the indexing
    starts at 1 by default. If offset is 0, and the positional parameters are used,
    $@ is prefixed to the list.
    

    由于这个答案获得了一些常规视图,让我添加一个解决John Rix 评论的可能性;正如他所提到的,如果您的字符串长度小于 3,${string: -3} 将扩展为空字符串。在这种情况下,如果您想要扩展 string,您可以使用:

    ${string:${#string}<3?0:-3}
    

    这使用了?: 三元if 运算符,可以在Shell Arithmetic 中使用;因为如文档所述,偏移量是一个算术表达式,这是有效的。


    符合 POSIX 的解决方案更新

    上一部分给出了使用 Bash 时的最佳选择如果你想针对 POSIX shell,这里有一个选项(不使用管道或外部工具,如 cut): p>

    # New variable with 3 last characters removed
    prefix=${string%???}
    # The new string is obtained by removing the prefix a from string
    newstring=${string#"$prefix"}
    

    这里要观察的主要事情之一是在参数扩展内部使用prefix 引用。 POSIX ref(在本节末尾)中提到了这一点:

    以下四种参数扩展提供子字符串处理。在每种情况下,应使用模式匹配表示法(参见模式匹配表示法)而不是正则表达式表示法来评估模式。如果参数是“#”、“*”或“@”,则扩展的结果是未指定的。如果未设置参数并且设置 -u 有效,则扩展将失败。将完整的参数扩展字符串括在双引号中不应导致以下四种模式字符被引用,而大括号内的引号字符应具有此效果。在每种模式中,如果省略单词, 应使用空模式。

    如果您的字符串包含特殊字符,这一点很重要。例如。 (破折号),

    $ string="hello*ext"
    $ prefix=${string%???}
    $ # Without quotes (WRONG)
    $ echo "${string#$prefix}"
    *ext
    $ # With quotes (CORRECT)
    $ echo "${string#"$prefix"}"
    ext
    

    当然,这只有在事先知道字符数的情况下才可用,因为您必须在参数扩展中硬编码?的数量;但在这种情况下,它是一个很好的便携解决方案。

    【讨论】:

    • 请注意,如果您的字符串短于您提供的负偏移量,这将不起作用。在这种情况下,您只会得到一个空字符串。
    • @gniourf_gniourf 如何将它与命令替换一起使用?我正在尝试提取主机名的最后三个字符deppfx@localhost:/tmp$ echo ${$(hostname): -3}-bash: ${$(hostname): -3}: bad substitution
    • @deppfx:你不能在 Bash 中。使用临时变量:temp=$(hostname); echo "${temp: -3}"。 Bash 还有HOSTNAME 变量(可能与hostname 的输出不同,也可能不同)。如果你想使用它,只需echo "${HOSTNAME: -3}"
    • @MichaelHays:只需分配函数的输出:string=$(some func),然后是 echo "${string: -3}"
    • @КонстантинВан 你不能在参数扩展中进行参数扩展(你不能嵌套它们!)。你必须为此使用一个临时变量。
    【解决方案2】:

    你可以使用tail:

    $ foo="1234567890"
    $ echo -n $foo | tail -c 3
    890
    

    获取最后三个字符的一种有点迂回的方法是说:

    echo $foo | rev | cut -c1-3 | rev
    

    【讨论】:

    • 当 bash 不可用时,“tail”在 dash 中也很有用。例如。新贵脚本部分。
    【解决方案3】:

    另一种解决方法是使用 grep -o 和一点正则表达式魔法来获取三个字符,然后是行尾:

    $ foo=1234567890
    $ echo $foo | grep -o ...$
    890
    

    为了使它有选择地获取最后 1 到 3 个字符,如果字符串少于 3 个字符,您可以使用 egrep 和这个正则表达式:

    $ echo a | egrep -o '.{1,3}$'
    a
    $ echo ab | egrep -o '.{1,3}$'
    ab
    $ echo abc | egrep -o '.{1,3}$'
    abc
    $ echo abcd | egrep -o '.{1,3}$'
    bcd
    

    您还可以使用不同的范围,例如5,10 来获取最后五到十个字符。

    【讨论】:

      【解决方案4】:

      1.广义子串

      为了概括 gniourf_gniourf 的问题和答案(因为这是我正在搜索的内容),如果您想将一个 范围 的字符从末尾的第 7 个字符剪切到第 3 个字符最后,您可以使用以下语法:

      ${string: -7:4}
      

      其中 4 是课程长度 (7-3)。

      2。替代使用 cut

      此外,虽然 gniourf_gniourf 的解决方案显然是最好和最整洁的,但我只是想添加一个使用 cut 的替代解决方案:

      echo $string | cut -c $((${#string}-2))-
      

      这里${#string}是字符串的长度,后面的“-”表示切到最后。

      3.使用 awk 的替代方法

      此解决方案改为使用 awk 的子字符串函数来选择具有语法 substr(string, start, length) 的子字符串,如果省略长度,则该子字符串将结束。 length($string)-2) 因此选择了最后三个字符。

      echo $string | awk '{print substr($1,length($1)-2) }'
      

      【讨论】:

      • 另一个答案中没有提到的变体是结合cut首先计算开始/停止的方法,然后在参数扩展中使用这些变量(还值得一提的是cut和bash 偏移量分别从 1 和 0 开始,因此需要将其计入计算中,我在这里不做):start=$((${#string}-3)); stop=$((${#string})); 然后echo ${string: $start : $stop} vs echo $string | cut -c "$start"-"$stop"
      • (哦,没关系 - 我看到我的评论没有解决字符串太短的问题,然后根本没有输出 - 剪切和字符串参数扩展。尽管如此,将其分解为变量(不必调用额外的命令)使其更易于阅读并且我认为仍然是一个好主意。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 2013-10-16
      • 1970-01-01
      • 2011-07-09
      • 2015-02-23
      相关资源
      最近更新 更多