bash 在 shell 脚本中嵌入 awk

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15020000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:38:12  来源:igfitidea点击:

Embedding awk in a shell script

bashawk

提问by Bertrand Caron

I've been using a bash script (script.sh) which uses various awk scripts (script1.awk,script2.awk) which are tailored at "runtime", by replacing values for instance. I've been looking for ways to embed them completely within the first bash script. Ideally, I would like to have a file looking like this :

我一直在使用 bash 脚本 (script.sh),它使用各种 awk 脚本 (script1.awk,script2.awk),这些脚本是在“运行时”定制的,例如通过替换值。我一直在寻找将它们完全嵌入第一个 bash 脚本的方法。理想情况下,我希望有一个如下所示的文件:

################################
# AWK scripts                  #
################################
read -d '' scriptVariable <<'EOF'
'
{my block commands;}
'
EOF
################################
# End of AWK Scripts           #
################################
awk $scriptVariable ${inputfile} # This line obviously doesn't work

instead of the traditional :

而不是传统的:

awk '{
my script commands
' ${intputfile}

Of course, I could write them to a file but the whole point is not to. Any suggestions ?

当然,我可以将它们写入文件,但重点不是。有什么建议 ?

EDIT : Although dogbane answers works fine, the next problem is that with the <<'HERE' tags, newline characters are not read.I can't unquote it since, otherwise, he's trying to interpret the awk script then encountering a $ sign within (and there are). And with no newlines, I can't comment anything within the awk script (without commenting half the script when the newlines characters are being removed ...). Anyone ?

编辑:虽然 dogbane 的答案工作正常,但下一个问题是,使用 <<'HERE' 标签时,不会读取换行符。我不能取消引用它,否则,他试图解释 awk 脚本然后遇到 $ 符号内(并且有)。并且没有换行符,我无法在 awk 脚本中评论任何内容(在删除换行符时不评论一半的脚本......)。任何人 ?

<< 'EOF'
BEGIN{#Hello
print 
}
EOF # Is read as BEGIN{#Hello print } by awk; ie BEGIN{

<< EOF
BEGIN{#Hello
print 
}
EOF #Is read correctly by awk but Bash tried to express  and fails

采纳答案by dogbane

Remove the single quotes around the awk script and enclose the script variable in double-quotes. This works for me:

删除 awk 脚本周围的单引号并将脚本变量括在双引号中。这对我有用:

################################
# AWK scripts                  #
################################
read -d '' scriptVariable << 'EOF'
BEGIN {
    print "start"
}
{
    print 
echo dummy code | awk '/dummy/{print '' }' - another_file | while read LINE; do
    case $LINE in
        " code")echo success;;
        *)echo $LINE;;
    esac
done
} END{ print "hello" } EOF ################################ # End of AWK Scripts # ################################ awk "$scriptVariable" ${inputfile}

回答by technosaurus

The single quotes around awk scripts can be cantankerous in a shell.

awk 脚本周围的单引号在 shell 中可能会很糟糕。

##代码##

This will remove from stdin and then another_file and output lines containing dummy and replace the first field with the first argument to your bash script. Notice that the bash variable $1 had to be unquoted.

这将从 stdin 中删除,然后从 another_file 和包含虚拟的输出行中删除,并将第一个字段替换为 bash 脚本的第一个参数。请注意,bash 变量 $1 必须不加引号。

回答by Axel

You can include your awk inside your bash script using a Here-Document.

您可以使用Here-Document将 awk 包含在 bash 脚本中。