以下 awk 命令打印 services 文件的结果
# awk '{print $2}' /etc/services
48002/tcp
48002/udp
48003/tcp
如何操作输出以便在每行之前打印“服务文件”?就像是:
# awk '{print $2}' /etc/services .......
service file 48002/tcp
service file 48002/udp
service file 48003/tcp
答案1
awk '{ print "service file", $2 }' /etc/services
为了避免出现空行和仅有注释的行:
awk '/^[a-z]/ { print "service file", $2 }' /etc/services