learn_gnuawk icon indicating copy to clipboard operation
learn_gnuawk copied to clipboard

Regular Expressions: Escape character

Open dtempcyer opened this issue 1 year ago • 3 comments

Hi, in the regular expressions chapter, I found an inaccuracy related to escaping characters in the incoming input.

Matching the metacharacters

$ echo '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
/learn/by/example

Using string literal as a regexp

# another example
$ echo '\learn\by\example' | awk '{gsub("\\\\", "/")} 1'
/learn/by/example
$ echo '\learn\by\example' | awk '{gsub(/\\/, "/")} 1'
/learn/by/example

In these examples, we don't escape the incoming text '\learn\\by\\example' with extra slashes and so echo returns the wrong string \learyample on pipe for awk.

dtempcyer avatar May 30 '24 15:05 dtempcyer

Backslashes are not special with default echo (at least on my system).

$ echo '\learn\by\example'
\learn\by\example

Your issue is probably mentioned in this thread: https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo

Edit: That said, I just checked it from my TUI apps and I see the behavior as you mention. I'll look into it further or may be just use printf and avoid all the trouble.

learnbyexample avatar May 31 '24 04:05 learnbyexample

Okay, thanks for the answer. Btw, I see the same behavior with printf.

dtempcyer avatar Jun 01 '24 13:06 dtempcyer

Ah, I meant printf '\\learn\\by\\example\n' so that it'll behave the same everywhere, unlike echo that differs in how it handles backslashes based on the implementation.

learnbyexample avatar Jun 01 '24 14:06 learnbyexample

I've fixed this and similar other examples to use printf instead of echo in version 2.5.

learnbyexample avatar Apr 07 '25 05:04 learnbyexample