Var in nawk script parse incorrectly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Var in nawk script parse incorrectly
# 1  
Old 03-24-2011
Var in nawk script parse incorrectly

I'm writing a Texas Hold'em script in bash v3.00.16(1) to learn more about awk/nawk scripts and regex expressions by trying to randomize a list of names using awk's rand function. The problem is that the elements in the var convert to a single element in the nawk script. I've tried several things, including separating the names with commas, single and double quotes but no joy.

The result is that ${Player_Names[0]} contains all the names and ${#Player_Names[@]} = 1.

I started with some code by C. F. Johnson that randomizes a deck of cards and wanted to alter that to randomize a list of player names. I replaced the regex expression {2,3,4,5,6,7,8,9,J,Q,K,A}_{Hearts,Spades,Diamonds,Clubs} that generates the deck of cards with a variable that contains a list of names. Note: In my variable there are 124 names.

Code:
xNAMES=( Abigail  Alejandro Alex Alton Amos )

get_players() # USAGE: get_players
{ 
Player_Names=$(
printf "%s\n" ${xNAMES[@]} |
  ${uBIN}/nawk '## Seed the random number generator
    BEGIN { 
    srand()
}
## Put a random number in front of each line
{ printf "%.0f\t%s\n", rand() * 99999, $0 }' |
  sort -n | ## Sort the lines numerically
  cut -f2 ## Remove the random numbers
)
# Player_1 is the user.
# Skipping 
Player_2=${xNAMES[2]}
Player_3=${xNAMES[3]}
Player_4=${xNAMES[4]}
Player_5=${xNAMES[5]}
Player_6=${xNAMES[6]}
  }

get_players

${BIN}/printf "\n\t${Player_1}\t${Player_2}\t${Player_3}\t${Player_4}\t${Player_5}\t${Player_6}\n"

What am I doing wrong? Do I have to replace the original regex expression (that works) with the list of names in the nawk script for it to parse correctly?

Thanks

Last edited by HexKnot; 03-24-2011 at 12:36 PM.. Reason: typo in code
# 2  
Old 03-24-2011
You're telling it to put it in a string, so you get a string. To put it in an array, tell it it's an array.
Code:
Player_Names=( $(
printf "%s\n" ${sNAMES[@]} |
  ${uBIN}/nawk '## Seed the random number generator
    BEGIN { 
    srand()
}
## Put a random number in front of each line
{ printf "%.0f\t%s\n", rand() * 99999, $0 }' |
  sort -n | ## Sort the lines numerically
  cut -f2 ## Remove the random numbers
) )

You can alter what character the array splits on by altering the shell's IFS variable. You might want IFS=$'\n' to split on newlines only. Be sure to put it back once you're done since it affects other things too.
# 3  
Old 03-24-2011
Sorry, I guess I'm missing something. I don't see any difference in what you posted and what I'm doing.

Code:
printf "%s\n" ${sNAMES[@]}

## Typo in original post has been fixed. sNames should have been xNames.

By putting parentheses around the names bash recognizes that the contents are an array. I can test this using the following.
Code:
$ xNAMES=( Abigail Alejandro Alex Alton Amos )
$ echo ${xNAMES[0]}
Abigail
$ echo ${xNAMES[1]}
Alejandro
$ echo ${xNAMES[2]}
Alex
$ echo ${xNAMES[3]}
Alton
$ echo ${#xNAMES[@]}
5

My problem is that the following happens when using the awk script.
Code:
xNAMES=( Abigail Alejandro Alex Alton Amos )
$ echo ${xNAMES[0]}
Abigail Alejandro Alex Alton Amos
$ echo ${xNAMES[1]}
$
$ echo ${xNAMES[2]}
$ echo ${#xNAMES[@]}
1
$

Please let me know if I missed something.

Thanks

Last edited by Franklin52; 03-31-2011 at 06:37 AM.. Reason: Code tags
# 4  
Old 03-24-2011
Quote:
Originally Posted by HexKnot
Sorry, I guess I'm missing something. I don't see any difference in what you posted and what I'm doing.
Look closer. I even highlighted the differences in red.
# 5  
Old 03-30-2011
Thanks for pointing that out. That did fix the problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk command to output in var

Hi I have this command, which counts number of lines in a specific file and then prints it on screen.nawk 'NF{c++}END{print "Number of GPS coordinates in file: "c}' $filename I would like to have the output put into a variable, but can't seem to find the correct argument for it. How do I... (3 Replies)
Discussion started by: bulleteyedk
3 Replies

2. Shell Programming and Scripting

Use of -r, grep, nawk in script

Hello Friends, I am new to unix shell, need to understand the meaning of few cmds in below script, #!/usr/bin/ksh FTP_FILE_NAME="$1" if then grep "TRLR@@@@@@" $FTP_FILE_NAME | nawk -F"" '{print $2}' else echo "0" fi what is the use of -r, grep, nawk & -F in above script. Why... (2 Replies)
Discussion started by: DK2014
2 Replies

3. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

4. Shell Programming and Scripting

Help nawk parse file

Hello, Hope you are doing fine. I have a file in following format. I only want to process the data inside the section that comes after #DATE,CODE,VALUE #ITEMS WITH CORRECTIONS ....... #DATE,CODE,VALUE 2011-08-02, ID1, 0.30 2011-08-02, ID2, 0.40 2011-08-02, ID3, 0.50 ...... Means... (3 Replies)
Discussion started by: srattani
3 Replies

5. Shell Programming and Scripting

Nawk Script not working

Any idea why this isn't working? YESTERF=`TZ=aaa24 date +%b"-"%d | sed 's/-0/--/'` filelist2=$(find /export/home/gen/cks/traces \( -name \*YESTERF\* -name \*DNA\* \) -print | tr '\n' ' ') print "Date/Time,Location,Shelf,IP,Reason,Log Filename" >> $OUTPUT nawk -F':' ' $2 ~... (2 Replies)
Discussion started by: ther2000
2 Replies

6. UNIX for Dummies Questions & Answers

Loop in Nawk Script

I have a script which performs a getline (customer enters data) and a list is returned which has the data that was entered return to them. Then it ends. How can I get this script to return to the begin and ask the question again. Ths script needs to stop after the list is returned and then hit... (2 Replies)
Discussion started by: Morph797
2 Replies

7. AIX

can't parse this nawk statement

hi all i have the following portion in an xml file: </n:AOMessage> <?xml version="1.0" encoding="UTF-8"?> <n:AOMessage xmlns:n="urn:ao:hs:update:shell" xmlns:bo="urn:ao:hs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ao:hs:update:shell... (0 Replies)
Discussion started by: chipahoys
0 Replies

8. UNIX for Dummies Questions & Answers

Parse /var/mail

Hi all... We have a box that receives a lot of incoming emails. I have a .procmailrc file that in turn invokes a python script, which process each of these incoming email. All is nice and good. However, before the .procmailrc and python script were in place, we still were receiving emails and... (0 Replies)
Discussion started by: khader69
0 Replies

9. Shell Programming and Scripting

how to parse the file in xml format using awk/nawk

Hi All, I have an xml file with the below format. <a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f> output needed is 111,222,333,123,234 111,222,333,456,789 nawk 'BEGIN{FS="<|>"} {print a,b,c,e,f a="" ... (7 Replies)
Discussion started by: natalie23
7 Replies

10. Shell Programming and Scripting

help need on nawk script

Dear experts I have a big file containing several profiles each flagged with "PROFILE" at the beginning of each one. I am trying to use the following command in cshell to seperate each profile and save each one in seperate file. I wrote a script as following: nawk -v i=0 '{if($1~/PROFILE/)... (5 Replies)
Discussion started by: Reza Nazarian
5 Replies
Login or Register to Ask a Question