How to use Awk to create a new field but retain the original field?

Clash Royale CLAN TAG#URR8PPP
How to use Awk to create a new field but retain the original field?
Can this be done in Awk?
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 (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
Logic to apply:
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”
Also, is this task possible with Sed?
cat
cat
Cat
My Catamaran sank
Cat
My friend is a real hound dog
With sed: possible, but strongly discouraged.
– Benjamin W.
Aug 7 at 15:38
Ed: I've edited the question.
– Steve_A
Aug 7 at 15:46
What's the expected output for
65765765|The Cat Sat on the Dog Bed|Dennis Smith? I feel like there's more requirements to this question than you've told us so far so I for one am reluctant to jump in and start guessing...– Ed Morton
Aug 7 at 16:39
65765765|The Cat Sat on the Dog Bed|Dennis Smith
Ed: If "first match" is found use that to set Nickname e.g. Cat Sat. Then ignore any further matches e.g. Dog.
– Steve_A
Aug 7 at 16:47
3 Answers
3
another awk
awk
$ awk 'BEGIN"
[Dd]og)",a);
$NF=(NR==1?"Nickname":a[1]) OFS $NF1' 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
I'm getting a syntax error at the "Match" command line.
– Steve_A
Aug 8 at 14:58
I think the array argument is
gawk specific. If you're using a different implementation you should use RSTART and RLENGTH to get the matched string out of the field.– karakfa
Aug 8 at 15:02
gawk
RSTART
RLENGTH
I'm using GNU Awk 4.2.1 (2018) on Mac OS X. Should the Match command work there?
– Steve_A
Aug 8 at 15:40
I think it should, not sure what the problem might be. Perhaps post the error message?
– karakfa
Aug 8 at 15:58
This might work for you (GNU sed):
sed -i '1s/|/&Nickname&/2;1b;s/|.*b(Cat|Dog)b.*|/&u1|/I;t;s/|.*|/&|/' file
Insert the column Nickname into the headings. If the second column contains either the word Cat or Dog insert a third column with the matching word in it. Otherwise insert a blank third column.
Nickname
Cat
Dog
You could try this with GNU awk:
awk
awk -F"|" -v OFS="|" 'NR==1$2 = $2 OFS "Nickname"
NR>1if($0 ~ /s*[Cc]at [Ss]ats+/) n="Cat"; else if($0 ~ /s*[dD]ogs+/)n="Dog";
else n=""; $2 = $2 OFS n 1' file
-F "|" OFS="|"
NR==1
NR>1
With the same logic, you could use this more compacted code:
awk -F"|" -v OFS="|" 'NR==1$2 = $2 OFS "Nickname"
NR>1n=($0 ~ /s*[Cc]at [Ss]ats+/) ? "Cat" : ($0 ~ /s*[dD]ogs+/) ? "Dog" : ""; $2 = $2 OFS n 1' file
Oh, I didn't see the edit, I'll update it.
– Gioconda
Aug 7 at 15:51
Updated with edits.
– Gioconda
Aug 7 at 16:02
Seems to work for "Dog" but not sure of correct syntax to use for "Cat Sat".
– Steve_A
Aug 7 at 16:45
Now it should work.
– Gioconda
Aug 7 at 18:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Does upper/lower case matter? If
catappears in Title then should the nickname in the output becatorCator nothing? What about partial matches, e.g. if Title isMy Catamaran sankthen it containsCatso should the nickname Cat be added? Does position matter? In your example it's always the 2nd word of Title where the keyword appears, is that always true? How aboutMy friend is a real hound dog? Think about edge cases and include THOSE in your sample input/output, not just the sunny day cases.– Ed Morton
Aug 7 at 15:38