awk modify string

Hi Guys,
i world like to do the following with awk,
i have the the complete username
example in a file a have some names

Mario Rossi
John Doe

i would like to convert this name in this format from file with awk

Mario,Rossi,"Mario Rossi [External]",m.rossi_ext@mydomain.com,$TRUE,
John,Doe,"John Doe [External]",j.doe_ext@mydomain.com,$TRUE,

Is it possible?

Thanks.

Hello charli1,

Could you please try following and let me know if this helps you(haven't tested it though).

awk '{print $0","s1 $1","$2"[External]" s1 ","tolower(substr($1,1,1))"."tolower($2)"_ext@mydomain.com,$TRUE,"}'  s1="\""  Input_file

Thanks,
R. Singh

1 Like

I get this output

Mario, Rossi,Mario,,Rossi[External],a.rossi@mydomain.com,$TRUE,

but i need this one,

Mario,Rossi,"Mario Rossi [External]",m.rossi_ext@mydomain.com,$TRUE,

thank a lot in advanced

Hello charli1,

Could you please try following and let me know if this helps you.

awk '{print $1","$2","s1 $0 "[External]" s1 ","tolower(substr($1,1,1))"."tolower($2)"_ext@mydomain.com,$TRUE,"}'  s1="\""  Input_file

Output will be as follows.

Mario,Rossi,"Mario Rossi[External]",m.rossi_ext@mydomain.com,$TRUE,
John,Doe,"John Doe[External]",j.doe_ext@mydomain.com,$TRUE,

Thanks,
R. Singh

1 Like

Great it worked like a sharm,
thank a lot mate.

---------- Post updated at 06:04 PM ---------- Previous update was at 12:36 PM ----------

That string worked just great,
but what if i have name that have three name,
for example

Mario Rossi
John Doe Michael
Mario De Rossi
Mario D'Rossi

could i also have the same format?

Mario,Rossi,"Mario Rossi [External]",m.rossi,m.rossi_ext@mydomain.com,$TRUE
John,"Doe Michael","John Doe Michael [External]",j.michael,j.michael_ext@mydomain.com,$TRUE
Mario,"De Rossi","Mario De Rossi [External]",m.derossi,m.derossi_ext@mydomain.com,$TRUE
Mario,"D'Rossi","Mario D'Rossi [External]",m.drossi,m.drossi_ext@mydomain.com,$TRUE

Is this possible?

Thank in advance.

Your desired output seems to be inconsistent and random. You say that you want:

Mario Rossi -> Mario,Rossi,"Mario Rossi [External]",m.rossi,m.rossi_ext@mydomain.com,$TRUE (1st initial"."last"_ext")
John Doe Michael ->John,"Doe Michael","John Doe Michael [External]",j.michael,j.michael_ext@mydomain.com,$TRUE (1st initial"."<middle removed>last"_ext")
Mario De Rossi -> Mario,"De Rossi","Mario De Rossi [External]",m.derossi,m.derossi_ext@mydomain.com,$TRUE (1st initial"."middlelast"_ext")
Mario D'Rossi ->Mario,"D'Rossi","Mario D'Rossi [External]",m.drossi,m.drossi@mydomain.com,$TRUE (1st initial"."middlelast<no "_ext">)

How is a program supposed to guess at when the middle name should be included in the login name and e-mail address versus when it should be excluded?
How is a program supposed to guess at when "_ext" should be included in an e-mail address and when it should be excluded?

Well, the problem is exactly that, withe the help of R. Singh
i already made this for the single first name and second name,

awk '{print $1","$2","s1 $0 " [External]" s1 ","tolower(substr($1,1,1))"."tolower($2)"" s2 ","tolower(substr($1,1,1))"."tolower($2)"_ext@octo.local,$TRUE,$Password,"}'  s1="\""   inputfile

my problem is how to do the same with those people that have multiple names and special caracters as ' , any suggestion is very welcome.

Removing non-alphabetic characters is easy with a gsub() call in awk . But knowing when the text before a space is to be thrown away and when it is to be included in the e-mail address is not something that Ravinder, I, or any other volunteer here can do for you unless you can explain the logic involved in making that decision.

1 Like