SavioZ
SavioZ

Reputation: 119

Sed - Invalid preceding regular expression

I have a problem with sed code. I wan't to do here:

From

uri.csv
/path/file.jpg
/path/file2.bmp

To

/path/*.jpg
/path/*.bmp

I'm use this code because I view the error with sed.

sed -r 's/(.+\/).+?(?=\.)(.+)/\\1*\\2/g' uri.csv
sed: -e expression #1, char 31: Invalid preceding regular expression

Can you help me?

Upvotes: 1

Views: 2689

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

Lookarounds are not supported by POSIX ERE that you are using (enabled with -r option).

Your regex matches one or more chars, as many as possible, up to / (with (.+\/)), then 1+ chars as few as possible are matched with .+?, then (?=\.) just requires a . to appear immediately on the right, and (.+) captures into Group 2 any 1+ chars as many as possible.

You may use

sed -r 's,(.*/)?.*\.,\1*.,' uri.csv

Or, with -E option:

sed -E 's,(.*/)?.*\.,\1*.,' uri.csv

Or using POSIX BRE:

sed 's@\(.*/\)\{0,1\}.*\.@\1*.@' uri.csv

See the online sed demo.

NOTE: When using , as delimiters, there is no need to escape / chars.

Upvotes: 2

Gilles Quénot
Gilles Quénot

Reputation: 185189

You can't use Look Around with sed. Better use a one-liner :

$ perl -pe 's/(.+\/).+?(?=\.)(.+)/$1*$2/g' file
/etc/designs/smartpos/images/*.svg
/etc/designs/smartpos/images/*.svg

Upvotes: 0

Related Questions