Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regular Expressions: Escape character #9

Open
alexwheezy opened this issue May 30, 2024 · 3 comments
Open

Regular Expressions: Escape character #9

alexwheezy opened this issue May 30, 2024 · 3 comments

Comments

@alexwheezy
Copy link

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.

@learnbyexample
Copy link
Owner

learnbyexample commented May 31, 2024

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.

@alexwheezy
Copy link
Author

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

@learnbyexample
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants