LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-03-2008, 07:50 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
how to grep all users's certain file


Can someone lead me how to do this. I have a bunch of users having .qmail in their home directory. How can I grep or cat whats inside in each of the user's .qmail file? I'm thinking of a bash script with 'for' syntax. Here is the sample:

Quote:
#!/bin/bash

users=`cat /etc/passwd | awk -F: '{print $1}'`

for id in $users
do
cd ~$id/ | cat .qmail
done
Now that I got all the list of users, didn't know how to cat/grep the .qmail file in each users. Please advise.


Thanks!
 
Old 01-03-2008, 08:02 PM   #2
GushpinBob
Member
 
Registered: Aug 2007
Distribution: File Server: Ubuntu 10.04
Posts: 56

Rep: Reputation: 15
change:

Code:
cd ~$id/ | cat .qmail
to
Code:
if [ -d ~${id} ] ; then
   cd ~${id}
   if [ -e ".qmail" ] ; then
      cat .qmail
   done
done
EDIT:
What this code modification should do (I hope) is that the script will go into the user's home directory (if it exists), and cat the .qmail file (if it exists).
Haven't tested it but give it a shot

Last edited by GushpinBob; 01-03-2008 at 08:07 PM. Reason: Explain code
 
Old 01-03-2008, 08:03 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Thanks will try that one.
 
Old 01-03-2008, 08:10 PM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
I think there's a mistake in your advice. You didn't put a fi.

Quote:
if [ -d ~id ] ; then
cd ~${id}
if [ -e ".qmail" ] ; then
cat .qmail
fi
fi
Though added an fi, still didn't work
 
Old 01-03-2008, 08:22 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F":" '
{ 
    path=$6"/.qmail"
    while ( ( getline line < path) > 0 ) {
          print line 
    }
    close( path )
}' /etc/passwd
 
Old 01-03-2008, 08:35 PM   #6
GushpinBob
Member
 
Registered: Aug 2007
Distribution: File Server: Ubuntu 10.04
Posts: 56

Rep: Reputation: 15
Quote:
Originally Posted by packets View Post
I think there's a mistake in your advice. You didn't put a fi.



Though added an fi, still didn't work
D'oh! My bad, I get the for loops and the if statements mixed up sometimes. Sorry that my solution didn't work anyhow.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I grep text from file? @ngelot Linux - Software 6 06-13-2007 04:44 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
grep throughout the whole file system....? vous Linux - Software 4 03-20-2005 01:30 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM
grep file in the subdirectory juno Linux - General 3 09-30-2002 11:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration