LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-11-2012, 11:57 AM   #1
roopakl
Member
 
Registered: Sep 2011
Posts: 92

Rep: Reputation: Disabled
grep help


Hi All,
I have made a small bash script file to make sure whether or not user's mail ID is stored in /root/.mail_id file.
Code:
# cat grep_user 
#!/bin/bash
user_id_file="/root/.users"
mail_id_file="/root/.mail_id"

user=( $(awk {'print $1'} $user_id_file) )
for i in ${user[@]}; do
/bin/grep $i: $mail_id_file >/dev/null 2>&1
while [ $? -gt 0 ]; do
echo "$i user not found in $mail_id_file file."
echo
done
done
All user names apart from those users to which password is locked are stored in /root/.users file.
Code:
# cat /root/.users 
user1
user2
user30
user_41
user_50
user-65
user-68
user78
user
some_more_user
The above user's mail ID is stored in /root/.mail_id file as below
Code:
# cat /root/.mail_id 
user1:user1@somedomain.com
user2:user2@somedomain.com
user30:user30@somedomain.com
user-65:user-65@somedomain.com
user_50:user_50@somedomain.com
user78:user78@somedomain.com
user_41:user_41@somedomain.com
user-68:user-68@somedomain.com
Let check whether is there any difference.
Code:
# cat /root/.users | wc -l
10

# cat /root/.mail_id | wc -l
8
So two user's entries are not in the /root/.mail_id file. So let check who are those
Code:
# bash grep_user 
user user not found in /root/.mail_id file.

some_more_user user not found in /root/.mail_id file.
Giving correct output as what I had expected. I added user entry as below
Code:
# echo "user:user@somedomain.com" >> /root/.mail_id

# cat /root/.mail_id 
user1:user1@somedomain.com
user2:user2@somedomain.com
user30:user30@somedomain.com
user-65:user-65@somedomain.com
user_50:user_50@somedomain.com
user78:user78@somedomain.com
user_41:user_41@somedomain.com
user-68:user-68@somedomain.com
user:user@somedomain.com
Then ran
Code:
# bash grep_user 
some_more_user user not found in /root/.mail_id file.
Now also giving correct output.
I replaced "user:user@somedomain.com" with "some_more_user:some_more_user@somedomain.com"
Code:
# sed -i 's/user:user@somedomain.com/some_more_user:some_more_user@somedomain.com/g' /root/.mail_id

# cat /root/.mail_id 
user1:user1@somedomain.com
user2:user2@somedomain.com
user30:user30@somedomain.com
user-65:user-65@somedomain.com
user_50:user_50@somedomain.com
user78:user78@somedomain.com
user_41:user_41@somedomain.com
user-68:user-68@somedomain.com
some_more_user:some_more_user@somedomain.com
Since I removed the entry of user:user@somedomain.com, now it should show for only "user" username. But it is not showing.
Code:
# bash grep_user

# echo $?
0

# cat /root/.mail_id 
user1:user1@somedomain.com
user2:user2@somedomain.com
user30:user30@somedomain.com
user-65:user-65@somedomain.com
user_50:user_50@somedomain.com
user78:user78@somedomain.com
user_41:user_41@somedomain.com
user-68:user-68@somedomain.com
some_more_user:some_more_user@somedomain.com

# cat /root/.users 
user1
user2
user30
user_41
user_50
user-65
user-68
user78
user
some_more_user
The username called "user" is there in /root/.users file but the entry is not in /root/.mail_id file. But why it is not showing as "user user not found in /root/.mail_id file as before".
I really appreciate your kind help.

Thanks & Regards

Last edited by roopakl; 09-11-2012 at 12:01 PM.
 
Old 09-11-2012, 12:21 PM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

Rep: Reputation: 23
its because "user" exists in every entry you can add ^ to indicate start of line and $ to indicate end of line

grep ^user$ /root/.users
 
1 members found this post helpful.
Old 09-11-2012, 12:43 PM   #3
roopakl
Member
 
Registered: Sep 2011
Posts: 92

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TheMadIndian View Post
its because "user" exists in every entry you can add ^ to indicate start of line and $ to indicate end of line

grep ^user$ /root/.users
Added "^"
Code:
/bin/grep ^$i: $mail_id_file >/dev/null 2>&1
Thank you very much TheMadIndian. you solved my problem

Last edited by roopakl; 09-11-2012 at 12:45 PM.
 
Old 09-11-2012, 02:06 PM   #4
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

Rep: Reputation: 23
Quote:
Originally Posted by roopakl View Post
Added "^"
Code:
/bin/grep ^$i: $mail_id_file >/dev/null 2>&1
Thank you very much TheMadIndian. you solved my problem
you're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
grep has no effect - does not grep anything in this for loopa LinuxChiq Linux - Newbie 2 12-01-2011 10:03 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 08:36 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration