0
BEGIN { 
       str = "Sorecheck.o"
       print str
       sub(".o", ".src",str)
       print str
    }

Output:

Sorecheck.o
.srcrecheck.o

Expected Output:

Sorecheck.o
Sorecheck.src

I wanted to replace ".o" with ".src" but here sub() is omitting "." character while comparing. can some one tell me how to fix this?

Myanju
  • 1,135
  • 1
  • 11
  • 23
  • Tried but no luck. Here is the output. Sorecheck.o .srcrecheck.o awk: main.awk:4: warning: escape sequence `\.' treated as plain `.' – Myanju Apr 16 '21 at 10:27
  • 2
    @Myanju, try changing it to `sub(/\.o/, ".src",str)` once? – RavinderSingh13 Apr 16 '21 at 10:32
  • 1
    @RavinderSingh13. It's working :) Can you please explain this? Thank you. – Myanju Apr 16 '21 at 10:39
  • 2
    @Myanju, why its working because we are making it as a regexp, if you put it inside `"` then you need to double escape it actually. – RavinderSingh13 Apr 16 '21 at 10:40
  • @RavinderSingh13 Tried with double escape it's working. What are the other characters needs to be escaped like this? – Myanju Apr 16 '21 at 10:44
  • 2
    @Myanju, may be you could check this link once https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions – RavinderSingh13 Apr 16 '21 at 10:46

0 Answers0