how to loop through each line of file?

Hi,

I’m trying to get a list of each logged on users and deal with each user separately. So what I’m thinking is save a list of all logged on users to a text file and extract each login name from each line in a loop.

So I’d do
Who > userList
TotalLoggedUsers=’who | wc –l’

Ok I know the total number of lines and I’ve got a list in a file
How do I loop through each line and extract the login name?

Thanks
LVL 2
sixpack434Asked:
Who is Participating?
 
brettmjohnsonConnect With a Mentor Commented:
# read the file userList one line at a time into username
while read username
do
  echo $username
done < userList

BTW.  You should do this:

who > userList
TotalLoggedUsers=`wc -l userList`

Note the correct use of backquotes.  Also note that TotalLoggedUsers is based the captured file.
Imagine someone logging in or out between your two invocations of 'who' command.
0
 
zephyr_hex (Megan)Connect With a Mentor DeveloperCommented:
cat userList will read through it... when you say "extract", what do you mean?  what are you trying to do with the login name?
also, i dont have a unix box in front of me, and i cant recall all of the data that gets returned from a Who.  you may need to use "cut" or "grep" to get the file contents so that it lists only the login names.
0
 
ahoffmannCommented:
man awk
man sed
man sh # or whatever shell you use
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sixpack434Author Commented:
I'm sorry none of your comments work,
I have a file that looks like this after running who > userList

user.user pts/0    Jun 17 17:40 (ac8f64e9.ipt.aol.com)
user1.user1 pts/1    Jun 17 20:54 (82-36-120-3.cable.ubr06.king.blueyonder.co.uk)
user2.user2 pts/3    Jun 17 20:54 (82-36-120-3.cable.ubr06.king.blueyonder.co.uk)

How do i exract the login name from each line for example 'user.user' from line 1?

the while read loop does read each line of a file but i want to extract the loginname from each line so that i can finger each user seperately
0
 
ahoffmannConnect With a Mentor Commented:
awk '{print $1}' userList
0
 
sixpack434Author Commented:
i'm afraid this doesn't work :''-( because what awk'{ print $1 }' is extract the first column from the userlist which are the username but i want to deal with each one seperately.

I'm trying to get the real names of each login so the script so far looks like

while read username
do
grep `awk '{ print $1 }'` /etc/passwd | cut -d: -f5
done < userList

but this doesn't work because `awk '{ print $1 }'` is not just one login name but all login names from who > userList

I want to deal with each one seperately so that i can get the real name from /etc/passwd
Helpppp
0
 
ahoffmannCommented:
you asked for:
==> How do i exract the login name from each line for example 'user.user' from line 1?
and my awk suggestion 100% does what you asked for!
now show something different which is not related to your first example anyhow, please open another question

BTW: man awk
2 days of try&error can save 42 minutes reading
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.