Storing information in arrays....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing information in arrays....
# 1  
Old 11-12-2008
Storing information in arrays....

Hello,

I am writing a shell script to do the following. It greps information from the messages log and then I use the cut command to isolate the field I need (the username) and output the information to a text file.

I now have to do the following. Count how many times each user logged in. So if the name "Mike" appears five times in the text file my script has to say Mike logged in five times.

It was suggested to me that I use awk to store the information from the text file in an array and process the information.

Though I am familiar with program design and concepts, I am rather new to shell scripting and awk and would ask the forum to provide me with some concrete code to help me understand what needs to be done.
# 2  
Old 11-12-2008
IF the file (inputfile in example below) has the first field = username (first field = $1 in awk)
Code:
awk '{ arr[$1]++; continue }
       END { for (i in arr) {print i, arr[i] }'   inputfile

awk uses associative arrays, before you ask.
# 3  
Old 11-12-2008
Quote:
Originally Posted by jim mcnamara
IF the file (inputfile in example below) has the first field = username (first field = $1 in awk)
Code:
awk '{ arr[$1]++; continue }
       END { for (i in arr) {print i, arr[i] }'   inputfile

awk uses associative arrays, before you ask.
Hello,

I used the code you provided as follows. In my shell script I have a line that calls the awk script:

awk -f processing.awk names.tmp

In processing.awk I modified your code as follows:
{ arr[$1]++; continue }
END { for (i in arr) {print i, arr[i] }

When the script runs it says:
awk: processing.awk:3: { arr[$1]++; continue }
END { for (i in arr) {print i, arr[i] }

awk: processing.awk:3: ^ syntax error

Can you help me troubleshoot the problem please?
# 4  
Old 11-12-2008

Use next, not continue.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

storing strings in C

i'm trying to figure out how to store strings into an array, can someone help me figure it out? i would like to store the following for example: array = "dog" array = "cat" the inputs are coming from the command line. Thanks, (6 Replies)
Discussion started by: l flipboi l
6 Replies

2. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

3. Programming

extracting information from lines, put them into arrays

hi I need a little help writing this small perl script. I'm trying to extract the values from each line in a file and find the average for example cat school Highschool 100, 123, 135 Middleschool 41, 67, 54 Elementary 76, 315, 384 ./average.pl highschool: 119.3 middleschool: 54... (2 Replies)
Discussion started by: gengar
2 Replies

4. Shell Programming and Scripting

Storing values in arrays

I have the following csh script which lets the use pass the following as an argument -legend=tag1/tag2/tag3/tag4/tag5/tag6/tag7 We pass a number of tags separated by '/'. I want to save the legend tags in an array and have done as below. How can I improve on this as things are getting quite... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Storing values in arrays using csh

I am reading a number of files but then I want to put the ranges xmin xmax ymin ymax as arrays for each file. Any idea how I can do this??? set j = 1 echo "Welcome $i times" while ( $j <= $i ) echo "$j" set fname = $fin-bst-misf.xy echo " "$fname ... (0 Replies)
Discussion started by: kristinu
0 Replies

6. Shell Programming and Scripting

Storing a password

Hello, for an automated telnet login script I need to store a password on the server. Is there a possibility to store the password in an encrypted form and decrypt it every time the login is performed? Are there any ideas Love, Sally (5 Replies)
Discussion started by: Sally[-_-]
5 Replies

7. Shell Programming and Scripting

Storing information into a database.

Hey guys, i am having a problem in storing new data into a text file. The database in in a text file and it displays information like this : Name : Price : Quantity Persia : 80 : 30 Now , i have written the code to check if the book already exist in the Databse. Number= echo -n... (3 Replies)
Discussion started by: gregarion
3 Replies

8. Shell Programming and Scripting

storing values in arrays using shell

Friends, I have to execute a command and store its contents into an array using shell. this is what i have tried #!/bin/bash disk_names = ($(`iostat -xtc | egrep -v "device|nfs" | awk '{print $1}'| tr '\n' ' ' `)) But its throwing an error message as ./test-script ./test-script:... (6 Replies)
Discussion started by: achak01
6 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies
Login or Register to Ask a Question