learn_gnuawk icon indicating copy to clipboard operation
learn_gnuawk copied to clipboard

Regular Expressions: Escape character

Open alexwheezy 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.

alexwheezy avatar May 30 '24 15:05 alexwheezy