Can you use heredocuments to embed AWK in a bash script?

后端 未结 6 1221
庸人自扰
庸人自扰 2021-02-05 15:54

Writing a shell script that pretty much wraps around an Awk script. I\'d like to have my syntax colouring on in the awk section so I feel there must be a better way to embed awk

6条回答
  •  伪装坚强ぢ
    2021-02-05 16:53

    If you don't want to use stdin for the data, do this --- it's portable, fast, and doesn't use any bashisms.

    awk -f- inputfile.txt <<'EOF'
    BEGIN {
      print "hello world"
    }
    EOF
    

    Telling awk to use an input filename of - causes it to read the script from stdin.

    Of course, you now have to store your data in a file. If you really can't do this, use a named pipe.

    f=/tmp/$$.data
    trap "rm -f $f" EXIT
    mknod $f p
    (df > $f) &
    awk -f- $f <

提交回复
热议问题