awk modify string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk modify string
# 1  
Old 01-13-2017
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

Code:
Mario Rossi
John Doe

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

Code:
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.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-13-2017 at 04:50 PM.. Reason: Added CODE tags.
# 2  
Old 01-13-2017
Hello charli1,

Could you please try following and let me know if this helps you(haven't tested it though).
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-13-2017
I get this output

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


but i need this one,

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


thank a lot in advanced

Last edited by charli1; 01-13-2017 at 06:55 PM..
# 4  
Old 01-13-2017
Hello charli1,

Could you please try following and let me know if this helps you.
Code:
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-13-2017
Great it worked like a sharm,
thank a lot mate.

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

Quote:
Originally Posted by RavinderSingh13
Hello charli1,

Could you please try following and let me know if this helps you.
Code:
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.
Code:
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
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?
Code:
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.

Last edited by charli1; 01-14-2017 at 06:36 AM..
# 6  
Old 01-14-2017
Your desired output seems to be inconsistent and random. You say that you want:
Code:
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?

Last edited by Don Cragun; 01-14-2017 at 08:00 AM.. Reason: Fix typo: s/<no "_ext"/<no "_ext">/
# 7  
Old 01-14-2017
Quote:
Originally Posted by Don Cragun
Your desired output seems to be inconsistent and random. You say that you want:
Code:
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_ext@mydomain.com,$TRUE (1st initial"."middlelast<no "_ext") I forgot to insert the _ext i corrected it now

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,
Code:
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.

Last edited by charli1; 01-14-2017 at 07:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

Modify xml using sed or awk

Hi All, I want to modify(changing the status from "on" to "off" status of Stage-element value from the below xml file using sed or awk: File Name: global.xml <?xml version="1.0" encoding="UTF-8"?> <config> <widget> <name>HTTP-POOL</name> <attributes> ... (5 Replies)
Discussion started by: wamqemail2
5 Replies

3. Shell Programming and Scripting

Modify text file using awk

I have text file with lines as shown here. Each row has 11 columns separated by tab. In each row, i want to split the 8th column such that the output should look like shown below. Here value in the 9th column is DP value and in the 10th column is MQ value followed by the values after resource.EFF=.... (15 Replies)
Discussion started by: mehar
15 Replies

4. Shell Programming and Scripting

Modify awk statement

how do i modify the following: echo "jaba law welcome no jaba law sorry now jaba law" | awk '{s+=gsub(/jaba law/,"jaba law")} END {print s}' so that it shows me the actual phrase it found matching the strings i specified? something like: jaba law jaba law jaba law (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

modify awk

awk "BEGIN {if($PERCENT<$WARNING) {print \"OK\" ; exit 0} else if(($PERCENT>=$WARNING) && ($PERCENT<$CRITICAL)) {print \"WARNING\" ; exit 1} else if($PERCENT>=$CRITICAL) {print \"CRITICAL\" ; exit 2} }" how can i... (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Shell Programming and Scripting

Modify an XLS file with Awk

Hello, I have 2 files. One has a list of serial numbers: 12345_7 2345_9 35454 4759:1 PEP8794 The other is an excel file, with multiple columns, separated by tab: 12345_7 ... ... .. .. .. .. .. 2345_9 ... ... .. .. .. .. .. 35454 ... ... .. .. .. .. .. 4759:1 ...... (4 Replies)
Discussion started by: ad23
4 Replies

7. Shell Programming and Scripting

awk can't modify the input file ??

Hi * I've just wanted to ask you if it's possible to modify the input file by using awk. Let me explain what I need: I need to change the value $4 from "defaults" to "nodev" in my text file. I've tried to use a string function called "sub" and it works. But I can't figure it out how to... (6 Replies)
Discussion started by: martinp111
6 Replies

8. Shell Programming and Scripting

modify and use awk sed program

The following awk script creates a file b.dat. awk '{print substr($0,1,27),substr($2,index($2,"_")+1)," ",substr($0,49)}' a.dat > b.dat I need this script to be modified to also sum $3 values by distinct $1 and $2 fields. Current file W2_2009275 2 8 W2_2009275 2 7 W1_2009275 1... (3 Replies)
Discussion started by: mnnarendra
3 Replies

9. Shell Programming and Scripting

Modify shell variables with AWK

Dear Folks, I have a command output something like: And I want to store PIN0 and SIG0 in two shell variables, now I do a double awk: PIN=`gsmctl -d /dev/ttyS0 pin sig | awk '/PIN0/ { print $2}'` SIG=`gsmctl -d /dev/ttyS0 pin sig | awk '/SIG0/ { print $2}'` It's possible to... (4 Replies)
Discussion started by: Santi
4 Replies

10. Shell Programming and Scripting

modify file using awk

I have a file, a.asc which is generated from a shell script: -----BEGIN PGP MESSAGE----- Version: PGP 6.5.8 qANQR1DBwE4DR5PN6zVjZTcQA/9z5Eg94cwYdTnC7v+JUegQuJwHripqnyjFrEs/ejzKYCNmngbHHmf8V4K3uFkYyp74aFf+CdymA030RKs6ewOwkmqRW19oIXCgVe8Qmfg+/2KTq8XN =0QSP -----END PGP MESSAGE----- I want... (12 Replies)
Discussion started by: nattynatty
12 Replies
Login or Register to Ask a Question