Solved

how to loop through each line of file?

Posted on 2006-06-15
10
1,780 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
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!

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 76
Unix / Linux grid computing 5 172
how to monitor remote shell execution on linux 9 113
wordappend challenge 8 249
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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