[clug] awk or Perl regex question

andrew at lists.savchenko.net andrew at lists.savchenko.net
Sat Jul 20 11:48:54 UTC 2019


Hi Steve,

On July 20, 2019 at 5:38 PM steve jenkin via linux <linux at lists.samba.org> wrote:

> In awk, I’m trying to remove First Names from Full Name strings.
> There might be multiple first names and alternative separated by a ‘/‘
> 
> Surnames as UPPERCASE and happen at the end of the string [and may contain single quote (O’SHEA) or  a blank (DE SMETS).

#
#!/usr/bin/awk -f
#
BEGIN {
   if (ARGV[1])
      print "\nSurnames from", ARGV[1], "\n";
   else
      exit 1
}

match($0, /[A-Z' ]*$/) {
      surname = substr($0, RSTART, RLENGTH);
      gsub(/^[ \t]+/,"",surname);
      gsub(/[ \t]+$/,"",surname);
      print surname;
}


Attaching the script and some names variations for a test (names.dat).


With Regards,
A


More information about the linux mailing list