Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For i in $userarray[@] is one line off

Status
Not open for further replies.

mac9968

IS-IT--Management
Apr 5, 2011
9
US
Hello,
I have searched the forum, but am unable to find direction on my issue.

I have s simple BASH script below
Bash:
#!/bin/bash
#
###########################
userarray=$(cat test|awk 'BEGIN {FS="\t"}; {print $1}'|sort -u)
echo "USERID              #of occurances"
for  i in $userarray[@]
do
printf "%s\t%s" "$i";grep -c $i test
done

This script cat's a file called test, pulls out the first field and pipes to sort -u so that only unigue userid show up. I then take that into a for loop, to execute a grep -c, so that I may know how many times a userid is in the file 'test'

Here is where the trouble comes in. The last userid has [@] attached as if it is part of the username and therefore finds no userid info the the file "test" Below is the output. (note the final userid is xr400709[@] and fouond "0" times
USERID #of occurances
#####################################
automated_account 6
ax435515 1
dq325123 3
dr343023 1
hk315305 15
jv806141 7
ke277981 2
mda3992 150
me062855 2
mshrestha 7
mt480449 5
ob717914 1
oh106034 1
ol620003 12
pk694319 26
qz800289 4
sh256270 2
uno1964 20
uno403b 3
uno995b 5
user_id 1
vi251929 1
wjc4842 87
xr400709[@] 0

Sorry for the lengthy post, thanks in advance for your help.
 

Remove the "[@]" from "for i in $userarray[@]", it's not necessary.
[banghead]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks LK, thought I needed that.....
the EMOTICON made me laugh out loud.[thanks]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top