什么 linux shell 命令返回字符串的一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/219402/
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-08-03 16:37:52  来源:igfitidea点击:

What linux shell command returns a part of a string?

linuxstringbashsubstr

提问by Binny V A

I want to find a linux command that can return a part of the string. In most programming languages, it's the substr()function. Does bash have any command that can be used for this purpose. I want to be able to do something like this... substr "abcdefg" 2 3- prints cde.

我想找到一个可以返回部分字符串的linux命令。在大多数编程语言中,它是substr()函数。bash 是否有任何可用于此目的的命令。我希望能够做这样的事情... substr "abcdefg" 2 3- 打印cde



Subsequent similar question:

后续类似问题:

采纳答案by Toybuilder

If you are looking for a shell utility to do something like that, you can use the cutcommand.

如果您正在寻找一个 shell 实用程序来执行类似的操作,您可以使用该cut命令。

To take your example, try:

以您的示例为例,请尝试:

echo "abcdefg" | cut -c3-5

which yields

这产生

cde

Where -cN-Mtells the cut command to return columns Nto M, inclusive.

Where-cN-M告诉 cut 命令将列返回NM, 包括。

回答by dmckee --- ex-moderator kitten

From the bash manpage:

从 bash 联机帮助页:

${parameter:offset}
${parameter:offset:length}
        Substring  Expansion.   Expands  to  up  to length characters of
        parameter starting at the character  specified  by  offset.
[...]


Or, if you are not sure of having bash, consider using cut.

或者,如果您不确定是否拥有bash,请考虑使用cut

回答by Juanma

In bash you can try this:

在 bash 你可以试试这个:

stringZ=abcABC123ABCabc
#       0123456789.....
#       0-based indexing.

echo ${stringZ:0:2} # prints ab

More samples in The Linux Documentation Project

Linux 文档项目更多示例

回答by Bill the Lizard

${string:position:length}

回答by camh

expr(1)has a substr subcommand:

expr(1)有一个 substr 子命令:

expr substr <string> <start-index> <length>

This may be useful if you don't have bash (perhaps embedded Linux) and you don't want the extra "echo" process you need to use cut(1).

如果您没有 bash(可能是嵌入式 Linux)并且您不想要使用 cut(1) 所需的额外“echo”进程,这可能很有用。

回答by ata

In "pure" bash you have many tools for (sub)string manipulation, mainly, but not exclusively in parameter expansion:

在“纯”bash 中,您有许多用于(子)字符串操作的工具,主要但不仅限于参数扩展

${parameter//substring/replacement}
${parameter##remove_matching_prefix}
${parameter%%remove_matching_suffix}

Indexed substring expansion (special behaviours with negative offsets, and, in newer Bashes, negative lengths):

索引子字符串扩展(具有负偏移的特殊行为,以及在较新的 Bash 中,负长度):

${parameter:offset}
${parameter:offset:length}
${parameter:offset:length}

And of course, the much useful expansions that operate on whether the parameter is null:

当然,对参数是否为空进行操作的非常有用的扩展:

${parameter:+use this if param is NOT null}
${parameter:-use this if param is null}
${parameter:=use this and assign to param if param is null}
${parameter:?show this error if param is null}

They have more tweakable behaviours than those listed, and as I said, there are other ways to manipulate strings (a common one being $(command substitution)combined with sed or any other external filter). But, they are so easily found by typing man bashthat I don't feel it merits to further extend this post.

它们具有比所列行为更多的可调整行为,正如我所说,还有其他方法可以操作字符串(一种常见的方法是$(command substitution)与 sed 或任何其他外部过滤器结合使用)。但是,通过打字很容易找到它们man bash,我觉得没有必要进一步扩展这篇文章。