2

Using sed, how to print the contents of the file with the last names and first names reversed?

Input file:

Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300

Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400

Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900

Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100

1 Answer 1

4
sed 's/\([[:alpha:]]\+\) \([[:alpha:]]\+\):.*/\2 \1/'

should work, provided that the first and last name do not contain spaces themselves.

EDIT: The above only prints the first and last name (reversed), to keep the rest of the entry, remove the :.*.

3
  • It only prints the last and first names reversed. how can I also print the rest of each line?
    – Steve
    Oct 8, 2012 at 19:03
  • Got it, I've tried this and it works: sed 's/([[:alpha:]]\+) ([[:alpha:]]\+):/\2 \1/' datebook
    – Steve
    Oct 8, 2012 at 19:07
  • Oh, I’m sorry, I thought you didn’t want this. Remove :.* to stop sed from dropping the rest of the line and maybe add an ^ to certainly only match the start of the line, like this: sed 's/^\([[:alpha:]]\+\) \([[:alpha:]]\+\)/\2 \1/'
    – Claudius
    Oct 8, 2012 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .