Sample Header Ad - 728x90

Difference between /RE/ and "RE" in awk

9 votes
1 answer
630 views
In awk, the first argument to the sub() and gsub() functions, the second argument to the match() function, and the optional third argument to split(), is an extended regular expression. Such an argument may be an arbitrary expression that evaluates to a string interpreted as a regular expression, or it may be a regular expression constant. In awk, a regular expression constant is written /RE/ for some regular expression RE (an "ERE" token in the awk grammar). **Question:** Assuming that RE remains the same (some non-variable regular expression), are there any practical differences between using /RE/ and using "RE" (a "STRING" token in the awk grammar) in a call to, e.g., sub()? Alternatively: is there any known awk implementation that distinguishes between these two ways of representing a regular expression in a call to the above-mentioned functions? The reason for asking is that I recall having to modify some awk code that tried to use "RE" as a regular expression in a call to either sub() or gsub(), because, for whatever reason, the awk implementation at hand did the wrong thing unless I called the function with /RE/. Unfortunately, this was some time ago (2+ years), so I don't remember the details, and I am even uncertain what Unix I used at the time (possibly OpenBSD), but I've been meaning to ask the question ever since.
Asked by Kusalananda (354278 rep)
Jan 17, 2023, 02:44 PM
Last activity: Apr 30, 2025, 09:21 AM