Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
@mddesai:
1 - I do believe the OP gave a rough example to explain the format. The amount of first/last names on a line is unknown and probably not limited to 3 pairs.
2 - What about the possible none unique entries that might be created.
Well I like druuna's idea, but I thought I would keep all counts starting at 1.
So infile used:
Code:
john smith jackie smith jill smith
Amitabh Bachchan Tom Hanks Brad Pitt
Deepika Padukone Anne Bachchan Priyanka Chopra
Code:
Code:
#!/bin/bash
declare -A uName
while read -a LINE
do
for (( x = 0; x < ${#LINE[*]}; x += 2 ))
do
(( x > 0 )) && echo -n ";"
tname="${LINE[x]:0:1}${LINE[x+1]}"
(( (( c = uName[$tname]++ )) > 0 )) || c=""
echo -n $tname$c
done
echo
done<infile
Don't forget those awkward Irish folk who insist in having apostrophes in their names either. And those pretentious double-barreled handles the posh folk tend to use.
<Initial><Surname> format login names is just all-round problematic and imo best avoided. If Patrick McGoohan had a login on one of my boxes I can tell you he'd be "user6" no matter how much he protested that "he wasn't a number"!
I'm not sure what your are trying to do. The script I posted uses a file as input (called infile). It is not build to except input from outside the script as shown by you.
The ksh used by me is the "original" Korn shell by AT&T, also known as ksh93:
Code:
ksh --version
version sh (AT&T Research) 93s+ 2008-01-31
Trying to use pdksh, for example, will not work as it doesn't know the -A typeset option (or in other words: doesn't allow associative arrays).
If you have a bash 4+ version on your machine you can use that as well (do change the shebang in my script). Older bash versions also cannot use associative arrays.
Last edited by druuna; 12-13-2013 at 10:30 AM.
Reason: fixed a typo
I tested the script which you have given and it works for all except when a name is like this firstname pe lastname or firstname de lastname and those weird name which have commas say firstname o'lastname any idea if we can get around with that or rather add them manually in system ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.