Solved

how to loop through each line of file?

Posted on 2006-06-15
10
1,775 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 (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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

14 Experts available now in Live!

Get 1:1 Help Now