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

后端 未结 6 1222
庸人自扰
庸人自扰 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:31

    Yes, in Bash this is possible in a slightly less ugly way:

    #!/bin/bash
    df | awk -f <(cat - <<-'EOD'
            BEGIN{
                print "test"
            }
            {print $3}
    EOD
    )
    

    The <() code block is replaced with a path (something like /dev/fd/63) which points to a file descriptor tied to the output of the code. Make sure the awk script is indented with tabs and not spaces (<<- strips leading tabs).

    Another way to do this is to hide the awk script at the end of the bash script like this (don't forget to exit):

    #!/bin/bash
    df | awk -f <(sed -e '0,/^#!.*awk/d' $0)
    exit $PIPESTATUS
    
    #!/usr/bin/awk -f
    BEGIN {
        print "test"
    }
    {print $3}
    
    0 讨论(0)
  • 2021-02-05 16:32

    ugly too:

    read -r -d '' awkscript <<'EOF'
    BEGIN{ print "test"}
    {print $3}
    EOF
    df | awk "$awkscript"
    
    0 讨论(0)
  • 2021-02-05 16:36

    Not sure what you mean, but are you looking for something like this?

    #!/bin/bash
    
    awk 'BEGIN {
        print "hello world"
    }
    { 
        print $3
    }' <<< "$(df)"
    

    Have a look at 3.6.7 Here Strings in the bash manual

    0 讨论(0)
  • 2021-02-05 16:45

    Sometimes this may be usefull:

    awkscript=$(cat << EOT
    BEGIN{
        print "test"
    }
    {print $3}
    EOT
    )
    
    df | awk "$awkscript"
    
    0 讨论(0)
  • 2021-02-05 16:47

    One way is to let awk read rules from stdin using (heredoc or here string) but process real files or files created by subprocess substitution, like this

    awk -f- <(df) <<'EOF'
    BEGIN{
        print "test"
    }
    {print $3}
    EOF
    

    -f- should appear before <(df). The delimiter for the heredoc should be quoted so that the $ signs will be passed into awk instead of being expanded.

    0 讨论(0)
  • 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 <<EOF
    { print }
    EOF
    
    0 讨论(0)
提交回复
热议问题