Matt
Matt

Reputation: 153

Removing spaces to restructure an email address

I am trying to restructure a text file that has email address to be compliant with email standards.

INPUT

fistname1 lastname1 <  [email protected]  >
fistname2 lastname2 othername2 <  [email protected]  >
fistname3 lastname3 could be more than one name <  [email protected]  >

desired output

fistname1 lastname1 <[email protected]>
fistname2 lastname2 othername2 <[email protected]>
fistname3 lastname3 could be more than one name <[email protected]>

I have tried some awk but it keeps failing.

Upvotes: 1

Views: 234

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15368

Try it with sed's -Extended pattern matching. Easier to read.

sed -E 's/<\s+/</; s/\s+>/>/;' file
fistname1 lastname1 <[email protected]>
fistname2 lastname2 othername2 <[email protected]>
fistname3 lastname3 could be more than one name <[email protected]>

If it's a really big file, you might save a little time doing it all in one pattern

sed -E 's/<\s*(\S+)\s*>/<\1>/;' file

(As an aside, I have decided that I, too, shall name my fists. ;)

Upvotes: 0

anubhava
anubhava

Reputation: 785481

Using sed you can do this:

sed -E 's/<[[:blank:]]*([^[:blank:]]+)[[:blank:]]*>/<\1>/g' file

fistname1 lastname1 <[email protected]>
fistname2 lastname2 othername2 <[email protected]>
fistname3 lastname3 could be more than one name <[email protected]>

:: Command Details ::

Match:

  • <: Match <
  • [[:blank:]]*: Match 0 or more whitespaces
  • ([^[:blank:]]+): Match 1+ non-whitespace characters and capture in group #1
  • [[:blank:]]*: Match 0 or more whitespaces
  • >: Match >

Replacement:

  • <\1>: Replace with back-reference of group #1 wrapped with < and >

Upvotes: 3

Related Questions