Hi,
I have some output in the form of:
#output:
abc123
def567
hij890
ghi324
the above is in one column, stored in the variable x ( and if you wana know about x... x=sprintf(tolower(substr(someArray[2],1,1)substr(userArray[1],3,1)substr(userArray[4],2,1)))
when i simply print x (print x) I get the above output. so it work.
I want to make an array of the above output so that each single username (abc123) is stored in a new array . (ex: usrArray[1]="abc123" and usrArray[2]="def567")
So I use the split function as:
split(x,usrArray,"\n");
well... when I print usrArray[1], instead of just giving me abc123, i get the same results of the variable x, or sometimes different ...
any help ?
regards
Set FS to "\n" before using the split function.
Regards
Hi, frank, below is my code:
the Input file is in the format of:
"ADLER, CHARLES DAVID" 00-9388 x0753 Engineering
and the list goes on n on....
My code is:
BEGIN
{ FS="[\t]";
OFS=" ";
}
{
# if some values in Array3 are null, use 'x' instead. Works !
split($1,userArray," ");
}
{
for (i=1;i<=NR;i++)
if (userArray[3] == "")
userArray[3]="x"
}
{
# get the usernames output desired: it works.
x=tolower(substr(userArray[2],1,1)substr(userArray[3],1,1)substr(userArray[1],2,1)substr($4,1,3));
}
{
#get the GECOS from file ! Works.
comment=sprintf(substr(userArray[2],1)" "substr(userArray[3],1,match(userArray[3],"\"")-1)" "substr(userArray[1],2,match(userArray[1],",")-2)" "$2 " "$3" "$4);
}
function addUser()
{
#start of function - still working on it
#for (x=1;x<=length(userz);x++)
#dept=substr(userz[x],3);
#if [ "dept" == "eng" ]
#useradd -c arrayHoldingGECOS -g Engineering -d/home/Engineering user[x]
#to check: cat /etc/passwd
#useradd -c user[x]
echo "uncomplete"
#end of function
}
{
#make an array, so each user names goes to a diff element in the users array
split(x,users);
print users[1];
}
Help would be appreciated,
regards
What should be the username, arrayHoldingGECOS of the given record?
"ADLER, CHARLES DAVID" 00-9388 x0753 Engineering
Regards
Usernames should be:
user[1]="cdaeng";
user[2]="rdasal"; and so on....
while GECOS is just the first name and last name and phone and stuff (which goes to user comments)... if i get the usernames working, GECOS will be the same process.
try running the awk file on a file which has the following fields:
"CAPOZZI, MICHAEL B" 08-3191 x8035 Manufacturing
"CLAYPOOLE, AARON JON" 00-8739 x4424 Engineering
"CORRIGAN, DANIEL W" 09-5501 x4673 Sales
and so on.
Try this approach to see if you get the desired output:
awk 'BEGIN {FS="\""}
{ n=split($2,aname," ")
for(i=2;i<=n;i++) {
user=user substr(aname,1,1)
comment=aname
}
comment=comment " " aname[1]
gsub(",","",comment)
user=user substr($2,1,1)
n=split($3,aname," ")
user=user substr(aname[n],1,3)
user=tolower(user)
comment=comment " " aname[1] " " aname[2] " " aname[3]
group=aname[n]
print "useradd -c " comment " -g " group " -d /home/" group "/" user
}' file
Regards
Hey ! thanx for the help, i'm just stuck at the last part now.
i'm in awk, so.
assume that:
userName = usernames
deptName = departname;
shell = shell name
gid = group ID
# the below part doesnt work:
system("useradd -c " deptName " " -h /home/"deptName " " -s " shell " -g " gid " -m -K PASS_MAX_DAYS=30 -K PASS_MIN_DAYS=3 " " userName).
could you fix that part ?
regards
Print the line like I did in the last example and see if you get the desired command.
If it's correct you can pipe the output of the script to the shell as follow:
./ scriptname | sh
Regards