Note that there are some explanatory texts on larger screens.

plurals
  1. POAwk processing of filenames containing backslash madness
    primarykey
    data
    text
    <p>I spent a whole day trying to process some files with backslashes and spaces inside their names. No matter what I do awk (gawk) refuses to print backslashes:</p> <pre><code>echo "this/pathname/contains/spa ces/and/back\\slashes" | xargs -d'\n' -n1 -I{} bash -c 'echo "{}"; echo whatever | gawk "{printf {}}"' this/pathname/contains/spa ces/and/back\slashes gawk: {printf this/pathname/contains/spa ces/and/back\slashes} gawk: ^ syntax error gawk: {printf this/pathname/contains/spa ces/and/back\slashes} gawk: ^ backslash not last character on line </code></pre> <p>This didn't work since the backspace gets directly into awk code.</p> <pre><code>echo "this/pathname/contains/spa ces/and/back\\slashes" | xargs -d'\n' -n1 -I{} bash -c 'echo "{}"; echo whatever | gawk "{printf \"{}\"}"' this/pathname/contains/spa ces/and/back\slashes gawk: warning: escape sequence `\s' treated as plain `s' this/pathname/contains/spa ces/and/backslashes </code></pre> <p>This worked, but awk eats the backslash. As you can see above, echo prints it but awk doesn't.</p> <pre><code>echo "this/pathname/contains/spa ces/and/back\\slashes" | ./escape.sh | xargs -d'\n' -n1 -I{} bash -c 'echo "{}"; echo whatever | gawk "{printf \"{}\"}"' this/pathname/contains/spa\ ces/and/back\slashes gawk: warning: escape sequence `\ ' treated as plain ` ' gawk: warning: escape sequence `\s' treated as plain `s' </code></pre> <p>Next I tried escaping the filenames using escape.sh</p> <pre><code>#!/bin/bash xargs -d'\n' -n1 -I{} bash -c 'echo $(printf "%q" "{}")' </code></pre> <p>Now there's a double backslash in there but awk still complains.</p> <pre><code>echo "this/pathname/contains/spa ces/and/back\\slashes" | ./escape.sh | xargs -d'\n' -n1 -I{} bash -c 'echo "{}"; echo whatever | gawk -v VAR=$(printf "%q" "{}") "{printf VAR}"' this/pathname/contains/spa\ ces/and/back\slashes gawk: ces/and/back\\slashes gawk: ^ syntax error gawk: ces/and/back\\slashes gawk: ^ unterminated regexp </code></pre> <p>Now awk said some nonsense about some unterminated regexp.</p> <p>Any ideas? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload