user121196
user121196

Reputation: 30990

use sed to search and replace patterns via regular expressions

trying to replace within a file everything including password up to the space.

password=xx%40%25PKz3L2jTa HTTP

tried the following sed command, using regular expression from http://en.wikipedia.org/wiki/Regular_expression

 sed 's/password=(.)+/ /g' $file

and

 sed 's/password=[^ ]+/ /g' $file

none of the above works. why?

Upvotes: 0

Views: 359

Answers (2)

Old Pro
Old Pro

Reputation: 25537

The standard sed does not understand "extended" regular expression, such as '+'. Two alternatives for you:

Enable extended regular expressions with

sed -E 's/password=[^ ]+/ /g' $file

or use standard regular expressions

sed 's/password=[^ ]*/ /g' $file

I prefer the second one because it is more portable and, unlike the first one, it will work with an empty password field.

Upvotes: 0

Vedran Šego
Vedran Šego

Reputation: 3765

Try with the -r argument:

sed -r 's/password=[^ ]+/ /g' $file

Upvotes: 2

Related Questions