Following on from this question about stripping newlines out of text, I want to turn this into a zsh alias as follows:
alias striplines=' awk " /^$/ {print "n"; } /./ {printf( " %s ",$0);}"'
I’ve tried escaping the quotes inside the awk script, but I’m getting this error:
awk: (FILENAME=bspsrobustness FNR=1) fatal: division by zero attempted
(The file is called bspsrobustness)
Is there a way to do what I want? I suppose I could turn this into an awk script rather than a zsh alias, is that my best option?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You could use a zsh function instead of an alias. No quoting hoops to jump through.
striplines() {
awk '... awk body "with quotes" ...' "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7d3b7">[email protected]</a>"
}
Method 2
To get an idea of what’s going on, run
% alias striplines='print -lr awk " /^$/ {print "n"; } /./ {printf( " %s ",$0);}"'
% striplines
awk
/^$/ {print "n"; } /./ {printf( " %s ",zsh);}
Since the $ characters are in double quotes (when they’re expanded after the alias is expanded), they are interpreted by the shell. To get the quoting right, it’s easier to put the whole alias definition in single quotes. What’s inside the single quotes is what will be expanded when the alias is used. Now that the argument of awk is surrounded in double quotes, it’s clear that you need backslashes before "$.
alias striplines='print -lr awk " /^$/ {print "n"; } /./ {printf( " %s ",$0);}"'
A useful idiom to single-quote a single-quoted string is that ''' is pretty much a way to put a literal single quote in a single-quoted string. Technically there’s a juxtaposition of a single-quoted string, a backslash-quoted ', and another single-quoted string. The juxtaposed empty string '' at the end can be removed.
alias striplines='print -lr awk ''' /^$/ {print "n"; } /./ {printf( " %s ",$0);}''
After this long explanation, a recommendation: when it’s too complicated for an alias, use a function.
Method 3
If you’re using zsh, you might also turn on the rcquotes option:
Allow the character sequence
''to signify a single quote within singly quoted strings. Note this does not apply in quoted strings using the format$'...', where a backslashed single quote can be used.
Thus, instead of
alias striplines='print -lr awk ''' /^$/ {print "n"; } /./ {printf( " %s ",$0);}''
using rcquotes you could write
setopt rcquotes
alias striplines='print -lr awk '' /^$/ {print "n"; } /./ {printf( " %s ",$0);}'''
or, by using $'...' you could write
alias striplines=$'print -lr awk ' /^$/ {print "n"; } /./ {printf( " %s ",$0);}''
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0