如何在bash中对字符串进行子串?

时间:2015-03-09 09:14:03

标签: linux shell substring

我想剪切以下字符串

ABCD | xyz的

管道之前和之后的字符数" |"符号可以变化。

我知道剪切命令,但它需要具体的数字'来自'和'到'而且我认为不会在这里工作。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

命令cut具有以下参数:

-d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
-f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified

接下来,以下命令应该按照您的意愿执行:

echo 'abcd|xyz' | cut -d'|' -f1   # Prints abcd
echo 'abcd|xyz' | cut -d'|' -f2   # Prints xyz