Solved

how to loop through each line of file?

Posted on 2006-06-15
10
1,774 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:sixpack434
10 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 25 total points
ID: 16916347
# 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
 
LVL 42

Assisted Solution

by:zephyr_hex
zephyr_hex earned 25 total points
ID: 16916360
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16918116
man awk
man sed
man sh # or whatever shell you use
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Author Comment

by:sixpack434
ID: 16927887
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 25 total points
ID: 16928890
awk '{print $1}' userList
0
 
LVL 2

Author Comment

by:sixpack434
ID: 16929279
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16930230
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now