LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-03-2010, 03:39 PM   #1
ineluki12
LQ Newbie
 
Registered: Dec 2004
Location: Missouri
Distribution: Gentoo 2004.3
Posts: 3

Rep: Reputation: 0
Trouble with grep extended regez in for loop


Hello,

I'm trying quickly dig through our VPN logs for a list of class A ips , and I'm having trouble. I have a file with a list of numbers, one per line. I'm wanting to incorporate each number into an extended regex:

Code:
for i in `cat ips`; do zgrep -E '[[:space:]]$i(\.[0-9]{1,3}){3}[[:space:]]' vpn1/2009.08.20.log.gz; done
If I substitute one of the numbers in for $i, it works for that number. For example:

Code:
for i in `cat ips`; do zgrep -E '[[:space:]]58(\.[0-9]{1,3}){3}[[:space:]]' vpn1/2009.08.20.log.gz; done
I think the loop is passing literally "$i" to zgrep instead of its value due to the tics, and I can't figure out how to resolve this. I can't remove the tics b/c zgrep doesn't like that. Putting a backslash in front of $i doesn't work. Any ideas?

Oh, and the number loop seems to be working fine:
Code:
for i in `cat ips`; do echo $i; done
1
14
27
43
58
59
60
61
110
111
[...]
Thanks for any assistance!

Last edited by ineluki12; 06-03-2010 at 03:40 PM.
 
Old 06-03-2010, 03:47 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Double quotes should work as well. And they permit shell's variable substitution:
Code:
for i in `cat ips`; do zgrep -E "[[:space:]]$i(\.[0-9]{1,3}){3}[[:space:]]" vpn1/2009.08.20.log.gz; done
Does this work for you?
 
Old 06-03-2010, 03:57 PM   #3
ineluki12
LQ Newbie
 
Registered: Dec 2004
Location: Missouri
Distribution: Gentoo 2004.3
Posts: 3

Original Poster
Rep: Reputation: 0
That worked beautifully. Thank you very much!
 
Old 06-03-2010, 03:58 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange error using grep in a while loop anil3 Linux - General 4 03-30-2009 03:09 AM
Grep in a for loop. rose_bud4201 Programming 13 03-18-2009 04:02 AM
Grep result in a loop and linefeed issue romainp Programming 2 06-13-2008 10:11 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 10:27 PM
problem with 'rpm -qa | grep' in while loop ionic_slim Programming 9 11-11-2007 08:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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