Awk脚本无法读取输入文件执行问题求助
Hey there, let's get your Awk script reading that roster file properly! The main issues with your current setup are how you're passing the file to Awk and a stray line in your script that doesn't do anything useful.
What's Wrong Now
- You aren't providing the roster file to Awk: When you run
awk -f script.awk, Awk executes the BEGIN block (which prints your header) but has no input file to process afterward—so it stops there. - The
rosterline in your script is invalid: Awk doesn't recognize that as a command to read the file; it just treats it as a string that doesn't trigger any action.
Corrected Script
First, update your script.awk to remove the unused roster line and format each input line to match your header's colon-separated structure. Adjust the field numbers ($1, $2, etc.) to match how your actual roster file is structured (I'm assuming the roster's columns align with your header order—tweak these if needed):
#!/bin/awk -f BEGIN { print "Last Name:First Name:Student ID:School – Major:Academic Level:ASURITE:Email" } { # Replace $1-$7 with the correct field positions from your roster file print $1 ":" $2 ":" $3 ":" $4 ":" $5 ":" $6 ":" $7 }
How to Run It
Pass the roster file as an argument to your Awk script like this:
awk -f script.awk roster
Or, if your script is executable (run chmod +x script.awk first), you can run it directly:
./script.awk roster
This will first print your header, then process each line of the roster file, formatting them into the colon-separated format you want.
内容的提问来源于stack exchange,提问作者kittensfurdays

