awk - split function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - split function
# 1  
Old 04-09-2008
awk - split function

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
# 2  
Old 04-09-2008
Set FS to "\n" before using the split function.

Regards
# 3  
Old 04-09-2008
Error Doesn't work...

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:

Code:
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
# 4  
Old 04-09-2008
What should be the username, arrayHoldingGECOS of the given record?

Code:
"ADLER, CHARLES DAVID" 00-9388 x0753 Engineering

Regards
# 5  
Old 04-09-2008
Hi

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.
# 6  
Old 04-09-2008
Try this approach to see if you get the desired output:

Code:
awk 'BEGIN {FS="\""}
{  n=split($2,aname," ")
   for(i=2;i<=n;i++) {
     user=user substr(aname[i],1,1)
     comment=aname[i]
   }
   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
# 7  
Old 04-09-2008
hey.

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split function in HP UNIX

hi all, i have large file. where i need the split the files into two files. can anyone tell me what is the command for that? Unix OS :HP Unix. Thanks, arun. (2 Replies)
Discussion started by: arun888
2 Replies

2. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

Perl split function

my @d =split('\|', $_); west|ACH|3|Y|LuV|N||N|| Qt|UWST|57|Y|LSV|Y|Bng|N|KT| It Returns d as 8 for First Line, and 9 as for Second Line . I want to Process Both the Files, How to Handle It. (3 Replies)
Discussion started by: vishwakar
3 Replies

4. Shell Programming and Scripting

The builtin split function in AWK is too slow

I have a text file that contains 4 million lines, each line contains 2 fields(colon as field separator). as shown: 123:444,555,666,777,888,345 233:5444,555,666,777,888,345 623:454,585,664,773,888,345 ...... Here I have to split the second field(can be up to 40,000 fields) by comma into an... (14 Replies)
Discussion started by: kevintse
14 Replies

5. Shell Programming and Scripting

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

6. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (0 Replies)
Discussion started by: castle
0 Replies

7. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

8. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

9. Shell Programming and Scripting

perl split function

$mystring = "name:blk:house::"; print "$mystring\n"; @s_format = split(/:/, $mystring); for ($i=0; $i <= $#s_format; $i++) { print "index is $i,field is $s_format"; print "\n"; } $size = $#s_format + 1; print "total size of array is $size\n"; i am expecting my size to be 5, why is it... (5 Replies)
Discussion started by: new2ss
5 Replies

10. UNIX for Dummies Questions & Answers

split function

Hi all! I am relatively new to UNIX staff, and I have come across a problem: I have a big directory, which contains 100 smaller ones. Each of the 100 contains a file ending in .txt , so there are 100 files ending in .txt I want to split each of the 100 files in smaller ones, which will contain... (4 Replies)
Discussion started by: ktsirig
4 Replies
Login or Register to Ask a Question