bash 使用 cut 命令从右侧提取的代码?

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

Code for extracting from the right side with cut command?

bashsubstringcut

提问by vfclists

There is a sample of using the cutcommand to extract parts of a string starting from the left. An example is given below.

有一个使用该cut命令从左侧开始提取字符串部分的示例。下面给出一个例子。

$ echo "abc-def-ghi-jkl" | cut -d- -f-2
abc-def

How can the same code be adapted to extracting from the right side? I thought of reversing the words and applying the same method, but it was too complicated.

相同的代码如何适应从右侧提取?本来想把字倒过来套用同样的方法,但是太复杂了。

回答by iruvar

you could use rev

你可以使用 rev

echo "abc-def-ghi-jkl" | rev | cut -d- -f-2 | rev

回答by John Kugelman

$ echo "abc-def-ghi-jkl" | cut -d- -f3-
ghi-jkl

-2cuts up to the 2nd field. 3-cuts from the 3rd field on.

-2削减到第 2 场。3-从第三场开始削减。