LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-13-2013, 12:43 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

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, 03: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 03:26 AM.
 
1 members found this post helpful.
Old 12-13-2013, 04:57 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,641

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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, 05:19 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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, 05:26 AM   #5
mddesai
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch
Posts: 485

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

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@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, 06:28 AM   #7
mddesai
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch
Posts: 485

Rep: Reputation: 133Reputation: 133
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, 07:10 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@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, 08:23 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,641

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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, 08:41 AM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
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, 10: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, 11:28 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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 11:30 AM. Reason: fixed a typo
 
Old 12-13-2013, 01: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, 09:18 AM   #14
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

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, 09:54 AM   #15
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,141

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:19 AM
Use full names from KAddressBook Aleksandersen Linux - Desktop 2 12-25-2007 11:35 AM
Full names on desktop? mehmetunlu Linux - General 1 02-14-2006 07:03 PM
How can I show the full names on my desktop? mehmetunlu Linux - Newbie 3 02-14-2006 07:55 AM
Full Names as IM ID strangehue General 12 11-27-2005 11:24 PM


All times are GMT -5. The time now is 12:46 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration