3

I want to substitute the pattern "uid=" followed with any single character one ore more times. So I use this command :

sed s/uid=.+/uid=something/g file

But this does not work. It seems that the "followed with any single character one ore more times" is not correct, that is to say .+

Any idea why?

2 Answers2

4

With BSD sed or recent versions of GNU and busybox sed, you may use

sed -E 's/uid=.+/uid=something/'

to match a single character one or more times.

The -E flag to sed enables extended regular expressions. Without it you get basic regular expressions. The two sets of regular expression grammars are largely the same but uses slightly different syntax, and the extended set supports more operators.

This would replace

uid=110

with

uid=something

If you want to match the same character --not with BSD sed--:

sed -E 's/uid=(.)\1*/uid=something/'

This would replace

uid=110

with

uid=something0

Standard EREs don't have back-references. GNU sed supports it as an extension, but not BSD sed. Back-references are a feature of standard BREs though, so you can do:

sed 's/uid=\(.\)\1*/uid=something/'

portably.

Kusalananda
  • 333,661
3
sed 's/uid=..*/uid=something/g' file

Or:

sed 's/uid=.\{1,\}/uid=something/g' file

for posix sed