LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-12-2013, 11:43 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Unhappy Ability to create usernames from full names.


Hi Gurus,

I want to create usernames from a file which has list of name in a format like

firstname lastname frstname1 lastname1 firstname lastname2

and output should be like something this
flastname;flastname1;flastname2

How can we get around with this ?
 
Old 12-13-2013, 02:24 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
hi,

to have all letters different I got this line instead:
'First Alpha Second Beta Third Omega Four'

Code:
$ echo 'First Alpha Second Beta Third Omega Four' | ( sep='' ; tr ' ' '\n' | while read first && read second ; do printf '%s%s%s' "$div" "$(printf '%s' "$first" | dd count=1 bs=1 2>/dev/null)" "$second" ; div=';' ; done ; echo )

FAlpha;SBeta;TOmega

Last edited by Ygrex; 12-13-2013 at 02:26 AM.
 
1 members found this post helpful.
Old 12-13-2013, 03:57 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
And what have you tried and where are you stuck?

Assuming set order this would seem to be rather easy in most languages.

If however the order of first and last names is possibly unknown this would be increasingly more difficult and you will need some form of identifier.
 
Old 12-13-2013, 04:19 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
The uniqueness of the created usernames should also be checked.
Code:
john smith jackie smith jill smith
All the above would end up as jsmith, which is probably not what you want.
 
Old 12-13-2013, 04:26 AM   #5
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Code:
awk '{print $1";"$3";"$5}' file
 
Old 12-13-2013, 04:45 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@mddesai: That only prints 3 first names and not first letter of first name combined with the last name for all entries in the line.

I had a quick go at it and came up with this (using a ksh shebang, a bash shebang can be used with bash 4+):
Code:
#!/bin/ksh

typeset -A uName

while read LINE
do
  nameArr=( ${LINE} )
  for (( x=0; x<${#nameArr[@]}; x=x+2 ))
  do
    tmpName="${nameArr[x]:0:1}${nameArr[x+1]}"
    [[ "${uName[$tmpName]}" != "" ]] && tmpName="${tmpName}$x"
    uName[$tmpName]="seen"
    echo -n "${tmpName};"
  done
  echo 
done < infile #> outfile
 
1 members found this post helpful.
Old 12-13-2013, 05:28 AM   #7
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by druuna View Post
@mddesai: That only prints 3 first names and not first letter of first name combined with the last name for all entries in the line.
Oh! Ok...

Then how about this...

Code:
$ cat names
Amitabh Bachchan Tom Hanks Brad Pitt
Deepika Padukone Anne Hathaway Priyanka Chopra 

$ awk '{print substr($1,0,1)$2";"substr($3,0,1)$4";"substr($5,0,1)$6}' names
ABachchan;THanks;BPitt
DPadukone;AHathaway;PChopra
 
Old 12-13-2013, 06:10 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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.
 
Old 12-13-2013, 07:23 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
Output:
Code:
jsmith;jsmith1;jsmith2
ABachchan;THanks;BPitt
DPadukone;ABachchan1;PChopra
 
1 members found this post helpful.
Old 12-13-2013, 07:41 AM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,901

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
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"!
 
1 members found this post helpful.
Old 12-13-2013, 09:45 AM   #11
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
druuna,

Code:
$ echo 'First Alpha Second Beta Third Omega Four' | ksh devel/ksh.ksh 
devel/ksh.ksh[3]: typeset: -A: unknown option
devel/ksh.ksh[8]: syntax error: '((' unexpected
I think this is common problem with bashisms

Code:
$ ksh -c 'echo $KSH_VERSION'
@(#)MIRBSD KSH R46 2013/05/02
 
Old 12-13-2013, 10:28 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Ygrex View Post
druuna,
Code:
$ echo 'First Alpha Second Beta Third Omega Four' | ksh devel/ksh.ksh 
devel/ksh.ksh[3]: typeset: -A: unknown option
devel/ksh.ksh[8]: syntax error: '((' unexpected
I think this is common problem with bashisms

Code:
$ ksh -c 'echo $KSH_VERSION'
@(#)MIRBSD KSH R46 2013/05/02
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
 
Old 12-13-2013, 12:52 PM   #13
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
that is why I tend to stick on POSIX if requirements are not clear enough, i.e. for all newbie questions
 
Old 12-15-2013, 08:18 AM   #14
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@ Thank you to all.

@Ygrex

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 ?
 
Old 12-15-2013, 08:54 AM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
cat sysmicuser.lst | tr "'" "-"
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Full Names Using Vim Editor? carlosinfl Linux - General 4 04-27-2010 07:19 AM
Use full names from KAddressBook Aleksandersen Linux - Desktop 2 12-25-2007 10:35 AM
Full names on desktop? mehmetunlu Linux - General 1 02-14-2006 06:03 PM
How can I show the full names on my desktop? mehmetunlu Linux - Newbie 3 02-14-2006 06:55 AM
Full Names as IM ID strangehue General 12 11-27-2005 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration