The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 05:26 AM
Read file then grep the line mr_bold Shell Programming and Scripting 2 02-06-2008 11:33 PM
How to read last line of a txt file? yongho UNIX for Dummies Questions & Answers 2 06-13-2005 10:20 AM
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 2 05-12-2005 10:13 AM
read line from file rein Shell Programming and Scripting 5 04-14-2005 11:32 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2005
Registered User
 

Join Date: Mar 2005
Posts: 4
Stumble this Post!
How to read from a file line by line and do stuff

I want to read from a file and then do some stuff based on the text read from each line. Basically it is a list of usernames and I need to read each user, check it exist in passwd and extract their home directory and then copy a few files to that users home directory. Then move onto the next user in the file. Is this possible ??
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-17-2005
Registered User
 

Join Date: Oct 2002
Posts: 670
Stumble this Post!
for i in `cat filename.txt`
do
grep $i /etc/passwd
etc
etc
done
Reply With Quote
  #3 (permalink)  
Old 03-17-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
Stumble this Post!
Quote:
Originally Posted by spaceship
I want to read from a file and then do some stuff based on the text read from each line. Basically it is a list of usernames and I need to read each user, check it exist in passwd and extract their home directory and then copy a few files to that users home directory. Then move onto the next user in the file. Is this possible ??
Here's a quick and sloppy way of doing it in KSH.
Code:
{
typeset IFS='
'
while read USER_NAME
do
   USER_DIR=$(grep $USER_NAME /etc/passwd | awk -F: '{print $6}')                                                                                                 
   print $USER_DIR                                                                                                                                                 
done < /etc/passwd
}
Reply With Quote
  #4 (permalink)  
Old 03-17-2005
Registered User
 

Join Date: Mar 2005
Posts: 4
Stumble this Post!
Cool

Thank you both for your help. Sorted
Reply With Quote
  #5 (permalink)  
Old 03-17-2005
Registered User
 

Join Date: Mar 2005
Posts: 48
Stumble this Post!
This will do

FileName='specify your file here'
while read LINE
do
echo $LINE
#do your stuff here
done < $FileName
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0