首页 > 解决方案 > Understanding awk command usage while passing a file content in it

问题描述

cat LIST.txt | awk 'BEGIN { print "POSTNT" ;} { print "NT Id= \""$1"\" id=\""$2"\" "}' | abc

Just to let everyone know:

Here $1 is actually the first column from cat LIST.txt and $2 is the second column from that file. The columns should be tab separated. This is not a question but a information.

标签: linuxbashawk

解决方案


你的命令,

cat LIST.txt | awk 'BEGIN { print "POSTNT" ;} { print "NT Id= \""$1"\" id=\""$2"\" "}' | abc

可能会稍微改进成

awk 'BEGIN { print "POSTNT" } { printf("NT Id=\"%s\" id=\"%s\"\n", $1, $2) }' <List.txt | abc

这只是摆脱了cat并且还使用printf而不是print。使用awk, 用于print打印单独的字段,如

print "some data", $1, $2, "some other data", $4

这将打印一个包含五个字段的记录。字段将由OFS(默认为空格)的值分隔,记录将由ORS(默认为换行符)的值终止。

但是,在这里,您可以格式化自己的字符串以进行输出,这就是所printf使用的。


推荐阅读