1:The idiom to do this is to cre" name="description" /> 1:The idiom to do this is to cre" />

Why can't I escape quote in gawk?


I'm trying to do the following, but either I'm way too tired and can't think, or something weird is hapening with the escapes:

scanimage -L | gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}' pipe bquote>


The idiom to do this is to create a variable which contains the single quote and then use that:

scanimage -L | gawk '/N650U/ {print gensub(q"`", "", "g", $2)}' q="'"

However, since you are using it in a character class, that is not going to work so you'll need to do this:

scanimage -L | gawk '/N650U/ {print gensub("[`'\'']", "", "g", $2)}' <-- 1st pair --> <-- 2nd pair -->

Another alternative if using bash is to use $'' which <em>does</em> support escaping single-quotes

scanimage -L | gawk $'/N650U/ {print gensub("[`\']", "", "g", $2)}'

All you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.

<h3>Example with single-quote in a regex</h3> $ echo $'foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1' f#### <h3>Example with single-quote outside a regex</h3> $ echo "foo" | awk '{print q$0q}' q="'" 'foo' <h3>Example with single-quote inside $''</h3> echo $'foo`\'' | awk $'{gsub(/[o`\']/,"#")}1' f####


There's <strong>no</strong> special character in <a href="http://www.gnu.org/software/bash/manual/bash.html#Single-Quotes" rel="nofollow">single quotes</a> including backslash(\).


Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.


You can change the command to:

$ scanimage -L | awk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'


Shell '...' doesn't support backslash escapes. You'll have to use "..." instead, I'm afraid.

gawk "/N650U/ {print gensub(\"['`]\", \"\", \"g\", \$2)}\"

(Note that shell "..." <em>does</em> expand $ variables, so you need to escape that as well!)


gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}' ^ ^ beginning of sq string end of single quoted string

A bit ugly, but this works:

gawk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'


The correct way to do this is simply:

scanimage -L | gawk '/N650U/ {print gensub(/[\047`]/, "", "g", $2)}'

It doesn't affect using \047 for a single quote but as an aside given all of the other posted solutions are using string delimiters, note the use of regexp delimiters instead of string delimiters around the regexp that is gsub()s first argument. It matters, see for example:

$ printf 'a\\tb\n' | awk '{sub(/\\t/,"X")}1' aXb $ printf 'a\\tb\n' | awk '{sub("\\t","X")}1' a\tb $ printf 'a\\tb\n' | awk '{sub("\\\\t","X")}1' aXb

The only time to use string delimiters in a regexp context is when you need to concatenate a literal with a variable, e.g.:

awk '{sub("<"var">","")}1'

Look up string and regexp delimiters in the man page for details.


  • How do you close open files using Swift?
  • Regex is not working as expected?
  • Custom URI routing by query string with CodeIgniter?
  • Is there a way to rename directories recursively without using sed or rename?
  • What's the benefit of the trailing apostrophe in character literals
  • python - terminate child process when script invoked from bash
  • python import fails when called from PHP
  • Proper way to add unescaped text from a field to a regex in postgres?
  • Color syntax highlighting in Bash while using R
  • Gem not installing package
  • SQL Server Nvarchar and Java prepared statement
  • Deploying pre-encrypted configuration files to a production environment
  • Sort by a column in a union query in SqlAlchemy SQLite
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • What does a hyphen at end of a term mean
  • How to run bash commands like “npm install” on complie
  • How to get the index of element in the List in c#
  • command line of process by name
  • Use sed with regex and (
  • Primefaces lazy datascroller calling load twice
  • How can I get the full list of running processes on a Mac from a python app
  • How to make R's read_csv2() recognise the text characters properly
  • SAXReader not re-ecape characters
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Get specific string
  • C: Incompatible pointer type initializing
  • Hardware Accelerated Image Scaling in windows using C++
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Can I make an Android app that runs a web view in Chrome 39?
  • How to make Safari send if-modified-since header?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to pass list parameters for each object using Spring MVC?
  • Compare two NSDates in iPhone
  • How to include full .NET prerequisite for Wix Burn installer
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Linking SubReports Without LinkChild/LinkMaster
  • JaxB to read class hierarchy