Help with sed regular expression

Hi all,

I want to get a substring from a string based on given delimiter, for example:

str="foo|bar|baz"

with delimiter "|",
I want to get one substring at each time with the order number the substring in the whole string,
given 1 to get "foo",
given 2 to get "bar",
given 3 to get "baz",
I guess it could be done with sed and an regular expression, but I don't know how to write it.

Thanks.
Roy987

Command cut would do the same job

echo $str | cut -d'|' -f1 # try -f2 or -f3
1 Like

Thanks.
It worked.