简体   繁体   English

已经使用单引号和双引号时,如何为 awk 添加引号?

[英]How to add quotes for awk when single and double quotes are already used?

I want to write an alias so that my squeue command outputs only the simulations that are running.我想写一个别名,以便我的 squeue 命令只输出正在运行的模拟。 For my squeue command, I've an alias in my.bashrc as follow对于我的 squeue 命令,我在 my.bashrc 中有一个别名,如下所示

alias sq='squeue -u as1056 --format="%.18i %.36j %.8u %.2t %.10M %.10L %.6D %.6C %.12P %R"'

With this, I can output the running simulations as有了这个,我可以 output 运行模拟为

sq | awk '$4 == "R" {print $0}'

This works as intended in the command line.这在命令行中按预期工作。 I now want to add this command as an alias我现在想将此命令添加为别名

alias sqrun='sq | awk "$4 == \"R\" {print $0}"'

As you can see, the single quotes are already exhausted after the equal to sign and hence, double quotes are needed after the awk command.如您所见,等号后的单引号已经用尽,因此在 awk 命令后需要双引号。 However, to match the pattern in column 4, I need to specify another string.但是,要匹配第 4 列中的模式,我需要指定另一个字符串。 Since single quote and double quotes have already been used, I'm not sure how I can specify a string in this scenario.由于已经使用了单引号和双引号,我不确定在这种情况下如何指定字符串。 I tried escaping the double quotes using backslash but it throws error:我尝试使用反斜杠 escaping 双引号,但它会引发错误:

awk: cmd. line:1:  == "R" {print -bash}
awk: cmd. line:1:  ^ syntax error

So how should I specify an alias in such scenario?那么在这种情况下我应该如何指定别名呢?

Edit1: There's a dirty way of doing it by matching a simple R pattern across the sq command. Edit1:通过在 sq 命令中匹配一个简单的 R 模式,这是一种肮脏的方式。 The problem is that it often matches columns which contain the word "DRAINED".问题是它经常匹配包含单词“DRAINED”的列。 So I can use sed to delete such lines.所以我可以使用 sed 来删除这些行。 So something like所以像

sq | awk "/R/ {print $0}" | sed "/DRAINED/d"

won't run into the problem of having 3 quotes in the command.不会遇到命令中有 3 个引号的问题。 But I want to avoid this approach as it's not generic and may give erroneous results if there ever happens to be a column containing R and not containing the word "DRAINED".但是我想避免这种方法,因为它不是通用的,并且如果碰巧有一列包含 R 并且不包含“DRAINED”一词,则可能会给出错误的结果。

PS: This is my first ever question on stackoverflow, so I'll appreciate your feedback if you notice any mistakes in the way I've asked this question! PS:这是我关于stackoverflow的第一个问题,所以如果您发现我提出这个问题的方式有任何错误,我将不胜感激!

you can chain single quoted sections by double quoting single quotes您可以通过双引号单引号链接单引号部分

alias sqrun='sq | awk '"'"' $4 == "R" {print $0} '"'"' '

added extra spaces for clarity but not needed.为了清楚起见,添加了额外的空间,但不是必需的。 However these nested quotes can become really hard to read and probable not a good idea except only for trivial cases and throw away scripts.然而,这些嵌套的引号可能变得非常难以阅读,并且可能不是一个好主意,除非只是用于琐碎的情况和丢弃的脚本。

The awk script contains $ and when you change the quotes around the awk script from ' to " , it makes bash treat them as variables to substitute. You have to also escape $ : awk 脚本包含$并且当您将 awk 脚本周围的引号从'更改为"时,它会使$将它们视为变量来替代。

alias sqrun='sq | awk "\$4 == \"R\" {print \$0}"'

I would go with a simple:我会用一个简单的 go :

alias sqrun='sq | awk '\''$4 == "R" {print $0}'\'

or with a function, like @DiegoTorresMilano suggested:或使用 function,如@DiegoTorresMilano 建议:

sqrun() { sq | awk '$4 == "R" {print $0}'; }

or if there are several aliases to define:或者如果有多个别名要定义:

#!/bin/bash

while IFS='' read -r line
do
    alias "${line%%: *}=${line#*: }"
done <<'EOF'
sq: 'squeue -u as1056 --format="%.18i %.36j %.8u %.2t %.10M %.10L %.6D %.6C %.12P %R"'
squn: sq | awk '$4 == "R" {print $0}'
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM