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 to get input from both file and the terminal? I wish to ask the user something in the END portion of the awk script.

That input should be typed in by the user and not read from the file which had the data to be processed.

END {
  getline choice
  if(choice == "Y")
    print "OK"
}

But choice is read off the input file.

share|improve this question
1  
awk was not really designed for interactive use. You can pass choice directly to awk like awk -f script.awk -v choice=Y, and use a wrapper shell script to do the interactive part. If you really need to wait until the END block to ask for choice, you could try getline < "/dev/tty" as a not terribly portable workaround. –  jw013 Nov 12 '12 at 15:58
add comment

1 Answer

up vote 4 down vote accepted

You can read from /dev/tty or from /dev/stdin.

getline choice < "/dev/tty"

/dev/tty is pretty ubiquitous (even one the very few, along with /dev/null and /dev/console to be required by POSIX), /dev/stdin is less common, but at least GNU awk would recognize it as meaning stdin even if the system doesn't have such a device/special file.

share|improve this answer
add comment

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.