LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2013, 01:08 PM   #1
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Rep: Reputation: Disabled
How to keep grep from using . in IP as regexp.


I have a script that is comparing IPs in two files. The problem i'm having is the grep is taking the dot in the IP as a regexp.

ip: 192.168.1.4
ip: 192.168.104.18

relevant portion of scipt.

Code:
if [ -e other_server_list ]; then
   cat file_2 |cut -f1 | sort -u | while read myTestIP
      do
         if grep -qw ${myTestIP} other_server_list; then
            grep -w ${myTestIP} Master_IP_File >> Mult_server_file
            grep -w ${myTestIP} other_server_list >> Mult_server_file
            eval sed -i '/"${myTestIP}"/d' file_2
            echo >> Mult_server_file
         fi
       done
else
    echo "oops"
fi
It is taking out both of the IPs listed above. How do i make it ignore the regular expression value of . (dot) without having to break the IP up by octets.

Running in a bash shell on CentOS 6.3.
 
Old 04-30-2013, 01:19 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
[schneidz@hyper ~]$ echo hello world | grep hello.world
hello world
[schneidz@hyper ~]$ echo hello world | grep hello\\.world
[schneidz@hyper ~]$ echo hello.world | grep hello\\.world
hello.world
 
Old 04-30-2013, 01:24 PM   #3
marc_online_
LQ Newbie
 
Registered: Apr 2013
Location: The Netherlands
Distribution: Linux Mint Cinnamon
Posts: 15

Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by schneidz View Post
Code:
[schneidz@hyper ~]$ echo hello world | grep hello.world
hello world
[schneidz@hyper ~]$ echo hello world | grep hello\\.world
[schneidz@hyper ~]$ echo hello.world | grep hello\\.world
hello.world

And it's seems even to work when using one backslash.
When executing in console.
Cheers.

Last edited by marc_online_; 04-30-2013 at 01:27 PM.
 
Old 04-30-2013, 01:34 PM   #4
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
The IP is in a variable not hard coded where i can put in a slash before the .
 
Old 04-30-2013, 02:03 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
use quotes and escape the . on the IPs?

Code:
export variable="123\.456\.123\.456"
echo "$variable"
123\.456\.123\.456
http://mywiki.wooledge.org/BashPitfa...lo_World.21.22 may have a clue.
 
Old 04-30-2013, 02:59 PM   #6
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
fgrep (or grep with -F or --fixed-strings)
 
2 members found this post helpful.
Old 04-30-2013, 03:59 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by ruario View Post
fgrep (or grep with -F or --fixed-strings)
You're "on Fire" again today!
 
Old 04-30-2013, 04:08 PM   #8
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Well it is my birthday today (no joke).
 
1 members found this post helpful.
Old 05-01-2013, 05:31 AM   #9
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ruario View Post
Well it is my birthday today (no joke).
Happy Birthday (belated now) and thanks.
 
Old 05-01-2013, 08:20 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
When using the console, remember that the console might consume one of the backslashes. A Unix shell recognizes patterns in command-line arguments, too.
 
  


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
Grep , Regexp teddyZA Linux - Software 7 10-02-2007 06:35 AM
grep regexp question mathfeel Linux - Software 1 11-28-2006 10:55 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 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
Open Source Consulting | Domain Registration