这可以在Awk中完成吗?
FILE_IN(输入文件)
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(所需结果)
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可以完成此任务吗?
答案 0 :(得分:1)
这可能对您有用(GNU sed):
sed -i '1s/|/&Nickname&/2;1b;s/|.*\b\(Cat\|Dog\)\b.*|/&\u\1|/I;t;s/|.*|/&|/' file
将列Nickname
插入标题。如果第二列包含单词Cat
或Dog
,则插入第三列,其中包含匹配的单词。否则,请插入空白的第三列。
答案 1 :(得分:0)
您可以使用GNU awk
进行尝试:
awk -F"|" -v OFS="|" 'NR==1{$2 = $2 OFS "Nickname"}
NR>1{if($0 ~ /\s*[Cc]at [Ss]at\s+/) n="Cat"; else if($0 ~ /\s*[dD]og\s+/)n="Dog";
else n=""; $2 = $2 OFS n} 1' file
-F "|" OFS="|"
分别指定定界符输入和输出。NR==1
处理标头大小写。NR>1
处理数据案例。按照相同的逻辑,您可以使用以下更紧凑的代码:
awk -F"|" -v OFS="|" 'NR==1{$2 = $2 OFS "Nickname"}
NR>1{n=($0 ~ /\s*[Cc]at [Ss]at\s+/) ? "Cat" : ($0 ~ /\s*[dD]og\s+/) ? "Dog" : ""; $2 = $2 OFS n} 1' file
答案 2 :(得分:0)
另一个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