Solved

how to loop through each line of file?

Posted on 2006-06-15
10
1,782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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