2

Is it possible for awk to read the program and the input from the standard input?

I would like to be able to pipe a file to the following function.

process_data () {
  awk -f - <<EOF
{print}
EOF
}

Note: the actual program is longer, it can't be passed as a command line argument, and I'd rather not use temporary files.

Currently it doesn't output anything.

$ yes | head | process_data 
$ 

2 Answers 2

9
process_data() {
  awk -f /dev/fd/3 3<< \EOF
  awk code here
EOF
}

Note that command line arguments can contain newline character, and while there's a length limit, it's general over a few hundred kilobyte.

awk '
  BEGIN {...}
  /.../ ...
  END {...}
'

If the issue is about embedding single quote characters in the awk script, another approach is to store the code in a variable:

awk_code=$(cat << \EOF
{print "'quoted' " $0}
EOF
)

And do:

process_data() {
  awk "$awk_code"
}
3

Why do you need to get the program from stdin? You could use single quotes ('), as Bash let's you split the contents between multiple lines.

# awk 'BEGIN { sum = 0 }
{ sum += $1 }
END { printf("sum = %d\n", sum) }' << EOF
1
2
3
EOF

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .