0

I have a bash script reading names from a webpage as variables, which use the format LASTNAME Firstname:

SANCHEZ Rick
SMITH Morty
VAN SOMETHING Halen

However, I need to display each name as Firstname LASTNAME:

Rick SANCHEZ
Morty SMITH
Halen VAN SOMETHING

I am looking for a way to recognize the lowercase capitalised first name and put it before the uppercase last name or names.

The closest I got was

echo $eachname | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'

This command however messes up people with two last names:

Rick SANCHEZ
Morty SMITH
Halen SOMETHING VAN

2 Answers2

0

This could be a sed possibility, given two or more last names and first names:

SANCHEZ Rick Ignatius Alexander
SMITH Morty
FOO VAN SOMETHING Halen

The command;

sed 's/^\([^a-z]*\)[[:blank:]]\(.*\)$/\2 \1/' file

Output:

Rick Ignatius Alexander SANCHEZ
Morty SMITH
Halen FOO VAN SOMETHING

Tell me if this works for you.

0

Or an alternative sed keeping middle names at the end

This input

SANCHEZ Rick
SMITH Morty
VAN SOMETHING Halen
MCSMITH Some Body

With this

sed -E "s/^([A-Z ]+) ([A-Z][a-z]+)(.*)/\2 \1\3/" file1

Gives this output

Rick SANCHEZ
Morty SMITH
Halen VAN SOMETHING
Some MCSMITH Body
bu5hman
  • 4,756