A first attempt would be
grep '^[^ ]* *[Kk]'
But this assumes that there is always exactly one firstname and no initials.
In this example you can use the -i
option and replace [Kk]
with just k
It might be better to latch on to the first colon
grep -i ' k[^:]*:'
If you really want to print just the lastname, and not the whole line, you should consider using awk (or perl)
Update: heres how the first grep expression '^[^ ]* *[Kk]'
is constructed
' apostrophe delimits a parameter that contains spaces
and other so-called meta-characters that the shell might alter
^ caret means start of line
[ brackets mark a set of characters, any one of which is to be matched
^ inside brackets means negation or 'none of the following'
so `[^ ]` means "not a space"
] is the end of the set.
* means 0,1 or more of the prior character
so `[^ ]*` means any contiguous group of characters that does not
contain a space
then we have two spaces
* means 0,1 or more of the prior character
so space space * means 1 nor more spaces.
[Kk] means `K` or `k`
[^:]* means 0,1 or more characters that are not a colon
: followed by a colon