You can do this in classic sed
notation with a couple of loops, one to fix dots in the first field, and one to fix dots in the second field.
sed -e ':f1' -e 's/^\([^ .]*\)\./\1\//' -e 't f1' \
-e ':f2' -e 's/^\([^ ][^ ]*\) \([^ .]*\)\./\1 \2\//' -e 't f2'
The ^
anchors are crucial to this working correctly. Yes, you can write it all on one line in a single argument to sed
; I prefer the clarity of separate arguments when the script is a complex as this. A typical sed
script is inscrutable enough without adding any extra obstacles to comprehension.
sed ':f1;s/^\([^ .]*\)\./\1\//;t f1;:f2;s/^\([^ ][^ ]*\) \([^ .]*\)\./\1 \2\//;t f2'
For your input sample (two lines), the output is:
SomeText/any_text/ch SomeText2/any_3/ch 5.6e-5
SomeText/any_text/ch something/else/point/separated/ch4 5.4e5
If you're using GNU sed
, you might need to add --posix
to the options, though it seemed to behave itself correctly (so it probably recognized that I wasn't using any non-POSIX notations and therefore stuck with POSIX).
Tested on Mac OS X 10.7.5 with BSD sed
and GNU sed
.
\(\)
and\1
..sed
(meaning portablesed
) uses\(...\)
; GNUsed
is non-standard and doesn't use that notation unless you ask it to with--posix
, but ... well, let's say that the point (one of the points) of having standards is to make it easy to move software around, and GNUsed
being non-standard makes it harder on everyone. (OTOH, POSIXsed
does not recognize\w
as a metacharacter sequence.)sed
makes it so difficult