LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-12-2012, 01:07 PM   #1
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Rep: Reputation: Disabled
grep multiple lines that the pattern matches in bash


Hi..All,
Here is small script, that can print if any pattern matches in the "mac" file,
Code:
#cat mac
192.168.0.20 a1:b1:c1:d1:e1:f1
192.168.0.25 a1:b1:c1:e1:e1:f1
192.168.0.27 a1:b1:c1:d2:e1:f1
192.168.0.32 a1:b1:d1:d1:e1:f1
192.168.0.67 a1:b1:c1:d1:e1:g1
192.168.0.72 a1:b1:c1:d1:g1:f1
192.168.0.83 a1:l1:c1:d1:e1:f1
192.168.0.84 a1:b1:c1:k1:e1:f1
192.168.0.92 d1:b1:c1:d1:e1:f1
192.168.0.120 a1:b1:c1:d2:e1:f1
192.168.0.125 a1:l1:p1:d1:e1:f1
192.168.0.138 a1:b1:c1:k1:y1:f1
192.168.0.185 d1:b1:r1:d1:e1:f1
192.168.0.208 a1:o1:c1:d2:e1:f1
Code:
#cat watch_arp 
#!/bin/bash
a="a1:b1:c1:d1:e1:g1"
b="a1:l1:p1:d1:e1:f1"
c="a1:b1:c1:k1:y1:f1"

for i in $a $b $c; do
x=`grep -i "$i" mac | awk {'print $2'}`
if  [ "$x" = "$a" ]; then
echo "Unknown user on the LAN is having a system's MAC address, these are the details
`grep -i "$i" mac`"
echo -e "\n"

elif [ "$x" = "$b" ]; then
echo "Unknown user on the LAN is having b system's MAC address, these are the details
`grep -i "$i" mac`"
echo -e "\n"

elif [ "$x" = "$c" ]; then
echo "Unknown user on the LAN is having c systems's MAC address, these are the details
`grep -i "$i" mac`"
echo -e "\n"

else
echo "Found nothing"
fi

done
If I run "bash watch_arp", it is giving the output that is what I really expected.

But if I add one more same mac address with different IP, i.e if I add 192.168.0.158 with same mac address of 192.168.0.138 in the mac file
Code:
192.168.0.138 a1:b1:c1:k1:y1:f1
192.168.0.158 a1:b1:c1:k1:y1:f1
then it is neither showing both IP info nor hiding "Found nothing" output, So please help me that what to modify in this.
Thanks in advance.

Last edited by niharikaananth; 03-12-2012 at 09:41 PM.
 
Old 03-12-2012, 01:41 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,422

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Could you explain a little further? You seem to already have repeated mac addresses before adding anymore.
 
1 members found this post helpful.
Old 03-12-2012, 09:40 PM   #3
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thanks for your kind reply grail,
Yes you are right there is a repeated mac address for both IP(192.168.0.138 and 192.168.0.158). Here is the output before adding the same MAC address of 192.168.0.138 with 192.168.0.158 i.e if I remove 192.168.0.158 line from mac file.
Code:
#bash watch_arp 
Unknown user on the LAN is having a system's MAC address, these are the details
192.168.0.67 a1:b1:c1:d1:e1:g1


Unknown user on the LAN is having b system's MAC address, these are the details
192.168.0.125 a1:l1:p1:d1:e1:f1


Unknown user on the LAN is having c systems's MAC address, these are the details
192.168.0.138 a1:b1:c1:k1:y1:f1
After adding same MAC of 192.168.0.138 with 192.168.0.158
Code:
#cat mac
192.168.0.20 a1:b1:c1:d1:e1:f1
192.168.0.25 a1:b1:c1:e1:e1:f1
192.168.0.27 a1:b1:c1:d2:e1:f1
192.168.0.32 a1:b1:d1:d1:e1:f1
192.168.0.67 a1:b1:c1:d1:e1:g1
192.168.0.72 a1:b1:c1:d1:g1:f1
192.168.0.83 a1:l1:c1:d1:e1:f1
192.168.0.84 a1:b1:c1:k1:e1:f1
192.168.0.92 d1:b1:c1:d1:e1:f1
192.168.0.120 a1:b1:c1:d2:e1:f1
192.168.0.125 a1:l1:p1:d1:e1:f1
192.168.0.138 a1:b1:c1:k1:y1:f1
192.168.0.158 a1:b1:c1:k1:y1:f1
192.168.0.185 d1:b1:r1:d1:e1:f1
192.168.0.208 a1:o1:c1:d2:e1:f1
if I run bash watch_arp getting the output as below
Code:
#bash watch_arp 
Unknown user on the LAN is having a system's MAC address, these are the details
192.168.0.67 a1:b1:c1:d1:e1:g1


Unknown user on the LAN is having b system's MAC address, these are the details
192.168.0.125 a1:l1:p1:d1:e1:f1


Found nothing
Now I wanted the output should be having also c system's MAC address i.e a1:b1:c1:k1:y1:f1 and without "Found Nothing" output. I mean the output should be like only as below
Code:
Unknown user on the LAN is having a system's MAC address, these are the details
192.168.0.67 a1:b1:c1:d1:e1:g1


Unknown user on the LAN is having b system's MAC address, these are the details
192.168.0.125 a1:l1:p1:d1:e1:f1


Unknown user on the LAN is having c systems's MAC address, these are the details
192.168.0.138 a1:b1:c1:k1:y1:f1

Unknown user on the LAN is having c systems's MAC address, these are the details
192.168.0.158 a1:b1:c1:k1:y1:f1
I hope now you understood and help me in this.

Last edited by niharikaananth; 03-12-2012 at 09:47 PM.
 
Old 03-12-2012, 10:08 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Are you using a non-Linux editor to add that extra line? Looks like it might be an invisible ctrl-char issue.
MS uses \r\n as a line ending, *nix uses \n.

In vi/vim you can use
Code:
:set list
to see/check the EOL char(s)

also, to get a blank newline, just use
Code:
echo
with no params.
 
1 members found this post helpful.
Old 03-12-2012, 10:39 PM   #5
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thanks chrism01 Guru,
I removed
echo -e "\n"
and added only
echo
But how to print 192.168.0.138 and 192.168.0.158 lines which is having the same mac address for both IP.
Since I don't have much knowledge in shell scripting I request you to help me in this.
 
Old 03-13-2012, 12:37 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
See my first comment; it looks like there's more there than meets the eye...
 
1 members found this post helpful.
Old 03-13-2012, 10:54 AM   #7
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
See my first comment; it looks like there's more there than meets the eye...
Hi..Guru,
Since I am newbie in shell scripting I don't know that how to use
Code:
:set list
So it would be great if you post that what to set(i/x)?
Thanks for your kind help.

Last edited by niharikaananth; 03-13-2012 at 10:58 AM.
 
Old 03-13-2012, 08:39 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Like I said, open up that file in vim and use the cmd
Code:
:set list
which will show you hidden/ctrl chars. It doesn't take any more params.

Another alternative is
Code:
od -c filename
where filename = mac in your case
http://linux.die.net/man/1/od
Post the results of the latter.
 
  


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
help with grep... only print specific string with multiple matches? trey85stang Linux - General 2 05-11-2009 10:29 AM
grep lines between occurances of a pattern ksri07091983 Solaris / OpenSolaris 5 04-26-2009 07:14 AM
Capturing only matches in grep/egrep via shell/bash koobi Programming 4 03-28-2009 01:37 PM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 10:19 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM


All times are GMT -5. The time now is 02:05 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