FilipMik
FilipMik

Reputation: 57

Unix: Cut string by regex delimiter

I have function that prints out the longest path in directory tree. Let's say the function prints this: ./.mozilla/firefox/z6upkljn.default/storage/permanent/chrome/idb/2918063365piupsah.files

What I want to do is to cut this string after match with user defined regex.

For example if user puts in regex like: *de?a*, the only match is z6upkljn.default. So at the end, the output will be ./.mozilla/firefox

Here is a code sample I found sed 's/My_expression.*//' Where the My_expression is regular expression and delimiter for cutting defined by user.

It works for this input $echo /homes/eva/xm/xmikfi00 | sed 's/mik.*//', where for output I get /homes/eva/xm/x. As expected.

But if I enter simple regex $echo /homes/eva/xm/xmifki00 | sed 's/mi?.*//', the output is /homes/eva/xm/xmikfi00. Anyone who can help me how to get the same output as in the previous example?

I'll be glad for any help or suggestions, thanks.

Upvotes: 1

Views: 6993

Answers (1)

Geoff Nixon
Geoff Nixon

Reputation: 4973

Sed uses (by default) POSIX BREs, not EREs. If what you're trying to match with your ? is "any character", use a .: echo /homes/eva/xm/xmifki00 | sed 's/mi..*//'.

See man 7 re_format for more details.

Upvotes: 1

Related Questions