如何使用Awk创建新字段但保留原始字段? [英] How to use Awk to create a new field but retain the original field?
本文介绍了如何使用Awk创建新字段但保留原始字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可以在Awk中完成吗?
Can this be done in Awk?
FILE_IN(输入文件)
FILE_IN (Input file)
ID_Number|Title|Name
65765765|The Cat Sat on the Mat|Dennis Smith
65765799|The Dog Sat on the Catshelf|David Jones
65765797|The Horse Sat on the Sofa|Jeff Jones
FILE_OUT(所需结果)
FILE_OUT (Desired Results)
ID_Number|Title|Nickname|Name
65765765|The Cat Sat on the Mat|Cat Sat|Dennis Smith
65765799|The Dog Sat on the Catshelf|Dog|David Jones
65765797|The Horse Sat on the Sofa||Jeff Jones
适用逻辑:
IF Title contains " Cat Sat " OR " cat sat " THEN Nickname = "Cat Sat" #same titlecase/text as was found#
IF Title contains " Dog " OR " dog " THEN Nickname = "Dog"
此外,Sed可以完成此任务吗?
Also, is this task possible with Sed?
推荐答案
另一个 awk
$ awk 'BEGIN{FS=OFS="|"}
{delete a;
match($2,"([Cc]at [Ss]at|[Dd]og)",a);
$NF=(NR==1?"Nickname":a[1]) OFS $NF}1' file
ID_Number|Title|Nickname|Name
65765765|The Cat Sat on the Mat|Cat Sat|Dennis Smith
65765799|The Dog Sat on the Catshelf|Dog|David Jones
65765797|The Horse Sat on the Sofa||Jeff Jones
这篇关于如何使用Awk创建新字段但保留原始字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文