linux - 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.
解决方案
你的命令,
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
使用的。
推荐阅读
- installation - 是否可以在 ubuntu 桌面操作系统中安装 openstack 控制器节点
- mysql - 排序规则 utf8_general_ci 不适用于 Heroku
- css - 为什么 html { font-size: 1rem; } 创建 1px 右边框?
- angular - 如何访问 matTabNavBar.updateActiveLink 方法
- java - 使用 YAML-CPP 将 YAML 值添加到标准映射
- python - 尝试在 Python 中使用 matplotlib 保存图形动画 - “无效文件对象:<_io.BufferedReader name=76>”
- powershell - 在 PowerShell 脚本中传递运行时参数
- go - 我必须发送数千条提醒,有什么办法可以避免每分钟出现一次自动收报机?
- python - 在熊猫数据框中将行折叠成一列值
- julia - UndefVarError 但变量已明确定义