我们有一个像这样的文件(包含更多行):
BeginJobID=S0065546 JESMSGLG(1/281) jname=CICWCMWD 队列=EXECUTION JESMSGLG(2/281) BeginJobID=S0065568 jname=CICWWUWD 队列=EXECUTION JESMSGLG(3/281) jname=CICWMCWD BeginJobID=S0065569 队列=EXECUTION JESMSGLG(4/281) jname=CICWTQ11 队列=EXECUTION BeginJobID=S0065599 BeginJobID=S0065600 JESMSGLG(5/281) 队列=EXECUTION jname=CICWFA11 JESMSGLG(6/281) jname=CICWFA21 BeginJobID=S0065601 队列=EXECUTION JESMSGLG(7/281) jname=CICWFY11 BeginJobID=S0065602 队列=EXECUTION BeginJobID=S0065603 JESMSGLG(8/281) jname=CICWFY21 队列=EXECUTION BeginJobID=S0065604 JESMSGLG(9/281) jname=CICWFQ11 队列=EXECUTION BeginJobID=S0065605 JESMSGLG(10/281) 队列=EXECUTION jname=CICWFT11 JESMSGLG(11/281) jname=CICWFT21 队列=EXECUTION BeginJobID=S0065606 JESMSGLG(12/281) jname=CICWFT31 队列=EXECUTION BeginJobID=S0065607 JESMSGLG(13/281) jname=CICWFT41 队列=EXECUTION BeginJobID=S0065608 BeginJobID=S0065609 JESMSGLG(14/281) jname=CICWGA11 队列=EXECUTION BeginJobID=S0065612 JESMSGLG(15/281) jname=CICWGA21 队列=EXECUTION JESMSGLG(16/281) BeginJobID=S0065613 jname=CICWGQ11 队列=EXECUTION BeginJobID=S0065614 JESMSGLG(17/281) 队列=EXECUTION jname=CICWGY11 BeginJobID=S0065615 JESMSGLG(18/281) jname=CICWGT21 队列=EXECUTION BeginJobID=S0065616 JESMSGLG(19/281) jname=CICWTT41 队列=EXECUTION JESMSGLG(20/281) BeginJobID=S0065617 jname=CICWGT11 队列=EXECUTION
我想知道一个 awk 简单命令来制作如下 2 个报告:
- 正在执行:
awk_simple_command_(jname=)
结果:
西太平洋岛屿发展中国家 西太平洋大学世界研究委员会 西太平洋岛屿妇女儿童基金会 CICWTQ11 CICWFA11 CICWFA21 CICWFY11 CICWFY21 CICWFQ11 CICWFT11 CICWFT21 CICWFT31 CICWFT41 CICWGA11 CICWGA21 CICWGQ11 CICWGY11 CICWGT21 CICWTT41 CICWGT11
- 正在执行:
awk_simple_command_(BeginJobID=)
结果:
S0065546 S0065568 S0065569 S0065599 S0065600 S0065601 S0065602 S0065603 S0065604 S0065605 S0065606 S0065607 S0065608 S0065609 S0065612 S0065613 S0065614 S0065615 S0065616 S0065617
答案1
如果您的输入数据文件包含仅由空格分隔的列,没有换行符,则可以使用 awk 解决问题:
报告.awk
BEGIN {
/* Split records on the space character */
RS=" ";
/* Within each record, split the components (fields) on the '=' character */
FS="=";
}
/* When the first field is the one requested (colname),
print the second field. */
$1 == colname { print $2; }
然后,假设您的数据文件名为“数据“,你可以像这样调用该程序:
$ awk-f 报告.awk 列名=jname数据
当然,使用 colname=名称或 colname=开始工作ID取决于您要提取的数据。这应该会产生您想要的输出。
如果您的数据文件有时使用换行符代替空格,您需要先将其转换为空格,然后将结果导入 awk:
$ 猫 数据|tr"\n" " " |awk-f 报告.awk 列名=开始工作ID-
如果您经常使用该命令,那么您当然可以将其粘贴在 shell 脚本中。
答案2
您可能还想考虑使用grep
:
grep -o 'jname=[^ ]\+' infile | grep -o '[^=]\+$'
和:
grep -o 'BeginJobId=[^ ]\+' infile | grep -o '[^=]\+$'