[英]AWK Escape Characters Confusion
So I've been doing some reading on AWK but I seem to be having a problem executing the following code: 所以我一直在做一些关于AWK的阅读,但是我似乎在执行以下代码时遇到问题:
curl http://website.com/users.csv | tac | tac | sed '101,400!d' | grep "female" |
awk -F',' '{ print "sudo useradd -Db /home/gender/female" " " $1 " " "-c" " " \"$5 " " $6\""}' |
bash
I'm trying to escape the double quotes immediately before $5
and after $6
so that I can either do a system call from AWK or just pipe to bash. 我试图在
$5
之前和$6
之后立即转义双引号,以便我可以从AWK进行系统调用,也可以仅管道传输到bash。 For some reason my escape characters are not working. 由于某些原因,我的转义字符不起作用。 I read this GNU AWK summary on escape sequences to no avail.
我没有阅读有关转义序列的GNU AWK摘要。
How can I accomplish this? 我该怎么做?
The output should look like this: 输出应如下所示:
sudo useradd -Db /home/gender/female dinessid1965 -c "Marina Propst"
And the input looks like this: 输入看起来像这样:
reatim,Shephoi8v,female,Ms.,Eija,Kankkunen,4721 Pearcy Avenue,Fort Wayne,IN,46804,,260-715-7242,2/22/84,Pisces,Dermatologist
Firstly, for the escaping: you make your life harder than necessary by quoting everything separately. 首先,为了避免转义:单独引用所有内容,会使您的生活变得比必要的更加艰难。 Instead of
代替
print "sudo useradd -Db /home/gender/female" " " $1 " " "-c" " " \"$5 " " $6\""
you could write 你可以写
print "sudo useradd -Db /home/gender/female " $1 " -c " \"$5 " " $6\""
at which point it becomes a little more obvious that there is something off with the quoting: GNU Awk complains that the \\
is not the last character of the line. 在这点上,引号似乎有些错了:GNU Awk抱怨
\\
不是该行的最后一个字符。
You could write it like this, with the escaped quote \\"
between quotes: 您可以这样写,在引号之间使用转义的引号
\\"
:
print "sudo useradd -Db /home/gender/female " $1 " -c \"" $5 " " $6 "\""
or, easier to read, specify a variable that contains the double quote (see the manual ): 或者,为了便于阅读,请指定一个包含双引号的变量(请参见手册 ):
awk -v dq='"' '{ print "sudo useradd -Db /home/gender/female " $1 " -c " dq $5 " " $6 dq }'
Secondly, your whole chain can be simplified: tac | tac
其次,你的整个链条可以简化为:
tac | tac
tac | tac
does nothing, and what grep and sed do can be done by awk: tac | tac
不执行任何操作,而grep和sed可以通过awk完成:
curl http://website.com/users.csv |
awk -F, -v dq='"' '/female/ && NR >= 101 && NR <= 400 { \
print "sudo useradd -Db /home/gender/female " dq $1 dq " -c " dq $5 " " $6 dq }' |
bash
Alternatively, to avoid some of the ugliness of adding spaces between variables using print
, we could use printf
(thanks to Ed Morton for the nudge): 另外,为避免使用
print
在变量之间添加空格的麻烦,我们可以使用printf
(感谢Ed Morton的推动):
curl http://website.com/users.csv |
awk -F, '/female/ && NR >= 101 && NR <= 400 { \
printf "sudo useradd -Db /home/gender/female \"%s\" -c \"%s %s\"\n", $1, $5, $6 }' |
bash
Notice how also the expansion of $1
is quoted in the last two commands to prevent side effects of characters special to the shell (or even malicious commands). 请注意,在最后两个命令中还如何引用
$1
的扩展名,以防止shell专用字符(甚至是恶意命令)产生副作用。
You're making this harder than necessary by involving awk at all. 通过完全使用awk,您将变得比必要的要难。 It's also dangerous because you're not quoting the contents of
$1
, $5
, and $6
for the shell. 这也是很危险的,因为您没有为shell引用
$1
, $5
和$6
的内容。 If they contain any funny characters, you're in trouble. 如果它们包含任何有趣的字符,则您会遇到麻烦。
You can do it entirely in bash, safely: 您可以完全安全地进行bash操作:
curl http://website.com/users.csv | sed '101,400!d' | grep "female" |
while IFS=, read -a words; do
sudo useradd -Db /home/gender/female "${words[0]}" -c "${words[4]} ${words[5]}"
done
Although for more safety you might need to ensure that the username field doesn't contain anything fishy (eg start with ../
). 尽管为了提高安全性,您可能需要确保用户名字段不包含任何可疑内容(例如,以
../
开头)。 I don't know how much checking useradd
does. 我不知道要检查
useradd
多少。
you're using too many quotes! 您使用了太多的报价!
Here is a simple example you can change into your format 这是一个简单的示例,您可以将其更改为格式
$ echo dinessid1965,two,three,four,Marina,Propst |
awk -F, -v q='"' '{print "sudo ...", $1, "-c", q $5, $6 q}'
sudo ... dinessid1965 -c "Marina Propst"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.