获得AWK用户输入 [英] get the user input in awk
本文介绍了获得AWK用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法读通过AWK编程用户输入?
我尝试写一个脚本来读取其中载有学生的姓名和ID的文件。
我得从通过键盘用户的学生的姓名,并通过使用AWK返回所有学生的成绩。
Is there any way to read the user input through the awk programming? I try writing a script to read a file which contained student's name and ID. I have to get the name of the student from the user through the keyboard and return all student's results by using the awk.
推荐答案
您可以使用函数getline
功能收集用户输入。请确保此设置在 BEGIN
块。这里的内容 script.awk
:
You can collect user input using the getline
function. Make sure to set this in the BEGIN
block. Here's the contents of script.awk
:
BEGIN {
printf "Enter the student's name: "
getline name < "-"
}
$2 == name {
print
}
下面是用的ID,名称和结果的示例文件:
Here's an example file with ID's, names, and results:
1 jonathan good
2 jane bad
3 steve evil
4 mike nice
样运行:
awk -f ./script.awk file.txt
这篇关于获得AWK用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文