Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 
$ 
share|improve this question

2 Answers 2

up vote 4 down vote accepted
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"
}
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.