Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sed to replace multiple characters 1

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have an input file that looks like this:

Code:
last_name     first_name mi  acct_nr
BOOP          BETTY      B   123456
FLINTSTONE    FRED           112233
MOUSE         MICKEY         234234
GRAPE         GOOFY      T   332233
What I need to have is a file that looks like this:

BOOP|BETTY|B|123456
FLINTSTONE|FRED||112233
MOUSE|MICKEY||234234
GRAPE|GOOFY|T|332233

I've tried to use

Code:
sed 's/  */|/g' DATA

where DATA is the current line and it almost works. The problem I have is that, for those names without a middle initial, it simply leaves out the field resulting, for example, in MOUSE|MICKEY|234234 . This is going to cause problems with the receiving system as it's expecting four (4) fields.

Thought/comments/hints appreciated.

As always, thanks to all in advance.

Tom

"My mind is like a steel whatchamacallit ...
 
You may try something like this:
awk 'NF==3{print $1"|"$2"||"$3}NF==3{print $1"|"$2"|"$3"|"$4}' DATA > newfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV:

Thanks for the hint. Unfortunately it doesn't work quite as I expected. Running the command(cut and pasted to avoid typos) results in two output lines in different formats. I can't post the actual data due to HIPAA but here's a 'sanitized' version of what is created (only the names and numbers have been changed to protest the innocent):

Code:
PIGG|PORKY||123456
PIGG|PORKY|123456^M|

And the output is only for those records without a middle initial - others (with middle initials) are ignored.

Tnx.

Tom

"My mind is like a steel whatchamacallit ...
 
Sorry for the typo:
awk 'NF==3{print $1"|"$2"||"$3}NF==[!]4[/!]{print $1"|"$2"|"$3"|"$4}' DATA > newfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV: Works GREAT!!!!

As a learning experience here, if they decide to add additional fields to the end of each record (say they want to add one more field, the Date of Service), what changes need to be made?

Thanks again.

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top