1

I have text file like this format:

...
SomeText.any_text/ch SomeText2.any_3/ch 5.6e-5
SomeText.any_text/ch something.else.point.separated/ch4 5.4e5
...

in line I have three elements: two - alpha-numerical-underscored-slashed strings and one - float number.

I need to replace points to slashes only at strings.

I have try to use sed with regular expression like this

sed 's/\([\w_]\+\)\(\.\)/\1\//g'

And don't have positive result.

3
  • y are u using \(\) and \1..
    – Anirudha
    Commented Oct 17, 2012 at 6:35
  • @Anirudha: (a) Please don't use SMS-ese on SO. (b) POSIX sed (meaning portable sed) uses \(...\); GNU sed 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 GNU sed being non-standard makes it harder on everyone. (OTOH, POSIX sed does not recognize \w as a metacharacter sequence.) Commented Oct 17, 2012 at 6:46
  • @JonathanLeffler sed makes it so difficult
    – Anirudha
    Commented Oct 17, 2012 at 6:48

4 Answers 4

1

This might work for you (GNU sed):

sed 's/[^ ]*$/\n&/;h;y/./\//;G;s/\n.*\n//' file

Explanation:

  • s/[^ ]*$/\n&/ insert a newline before the last field
  • h copy the pattern space (PS) to the hold space (HS)
  • y/./\// translate all .'s to /'s in the PS
  • G append a newline then HS to the PS
  • s/\n.*\n// remove everything between the first and last newlines i.e. delete the old strings

This idiom can be used to simplify changing part of a line without the need to resorting to complicated regexp's

1

Your elements look like fields. Therefore, my preferred method would be to use awk:

awk '{ for (i=1; i<=2; i++) gsub(/\./, "/", $i) }1' file.txt

Results:

SomeText/any_text/ch SomeText2/any_3/ch 5.6e-5
SomeText/any_text/ch something/else/point/separated/ch4 5.4e5
0

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.

0
awk '{gsub(/\./,"",$1);;gsub(/\./,"",$2);print}' your_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.