Question: I'd like to use `sed` to process a property file such as:

```
java.home=/usr/bin/java
groovy-home=/usr/lib/groovy
workspace.home=/build/me/my-workspace

```

I'd like to replace the `.`'s and `-`'s with `_`'s but only up to the `=`'s token. The output would be

```
java_home=/usr/bin/java
groovy_home=/usr/lib/groovy
workspace_home=/build/me/my-workspace

```

I've tried various approaches including using addresses but I keep failing. Does anybody know how to do this?

Answer: 
**awk** makes life easier in this case:

```
awk -F= -vOFS="=" '{gsub(/[.-]/,"_",$1)}1' file

```

here you go:

```
kent$  echo "java.home=/usr/bin/java
groovy-home=/usr/lib/groovy
workspace.home=/build/me/my-workspace"|awk -F= -vOFS="=" '{gsub(/[.-]/,"_",$1)}1'
java_home=/usr/bin/java
groovy_home=/usr/lib/groovy
workspace_home=/build/me/my-workspace

```

if you really want to do with sed (**gnu sed**)

```
sed -r 's/([^=]*)(.*)/echo -n \1 \|sed -r "s:[-.]:_:g"; echo -n \2/ge' file

```

same example:

```
kent$  echo "java.home=/usr/bin/java
groovy-home=/usr/lib/groovy
workspace.home=/build/me/my-workspace"|sed -r 's/([^=]*)(.*)/echo -n \1 \|sed -r "s:[-.]:_:g"; echo -n \2/ge'
java_home=/usr/bin/java
groovy_home=/usr/lib/groovy
workspace_home=/build/me/my-workspace

```
This works with any number of dots and hyphens in the line and does **not** require GNU sed:

```
sed 'h; s/.*=//; x; s/=.*//; s/[.-]/_/g; G; s/\n/=/'  < data

```

Here's how:

* h: save a copy of the line in the hold space
* s: throw away everything before the equal sign in the pattern space
* x: swap the pattern and hold
* s: blow away everything after the `=` in the pattern
* s: replaces dots and hyphens with underscores
* G: join the pattern and hold with a newline
* s: replace that newline with an equal to glue it all back together