Saturday, February 5, 2022

[SOLVED] Matching end of ine in GREP

Issue

I have this piece of bash script which is supposed to match words that end with an 'a'. However, when I run this, I get no output, despite the fact that my text file has words that end with 'a'.

cat $1 | cut -d'|' -f3 | cut -d',' -f2 | sed 's/^ //' | egrep -i "a$"

If I remove the '$' it shows output, but with '$' it returns nothing. It still works, just doesn't match.

Would appreciate some help with this. Thanks.

A sample of the file

MTNG1511|5013566|Xin, Mackenzie Darren MTNG9902|5079970|Park, Xue Hannah Vanessa MTNG1511|5059072|Chung, Michael Jia Tianyu MTNG1521|5060774|Lim, Stephanie Lauren MTNG1531|5060774|Lim, Stephanie Lauren MTNG2521|5060774|Lim, Stephanie Lauren MTNG9020|5060538|Bi, Samuel Shiyu MTNG9021|5060538|Bi, Samuel Shiyu MTNG9902|5072116|Hu, Kai Zhi Patrick

Output should be

Park, Xue Hannah Vanessa

Since it ends with an 'a'


Solution

There's probably extra whitespace at the end of your word.

Try adding

sed 's/[ \t]*$//' 

to remove the whitespace -- or else change your grep to allow for whitespace at the end.



Answered By - jdigital
Answer Checked By - Marilyn (WPSolving Volunteer)