LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-17-2008, 10:31 AM   #1
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
I am new to scripting


All i know is switches and routers but now, i have to do a bit of scripting on a SUN Solaris server.

I have two files Syslog and Inventory. Inventory has hundreds of IP addresses and its device name. Syslog is a subset of the Inventory which keeps changing every day. How do i get only those IPs and its corresponding device names.

eg:

How do i get
172.120.2.6 device6
172.120.2.2 device2
from the following files?


Syslog
172.120.2.6
172.120.2.2

Inventory
172.120.2.1 device1
172.120.2.2 device2
....
172.120.2.9 device9

Your help would be much appritiated.

Last edited by PKrishna; 03-17-2008 at 10:41 AM.
 
Old 03-17-2008, 10:46 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,745

Rep: Reputation: 299Reputation: 299Reputation: 299
Something like this:
Code:
for i in < Syslog; do grep -i -e $i; done
For each item in input from file Syslog; do get regular espression, ignore case, expression $i (the current item from Syslog); done
 
Old 03-17-2008, 10:58 AM   #3
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
hi thanks a lot for the quick reply,
i tried that but it gave me syntax error near unexpected token '<'
 
Old 03-17-2008, 11:34 AM   #4
jsurles
Member
 
Registered: Feb 2007
Location: Katy, TX
Distribution: gentoo, slackware, centos, ESX, gnu/linux
Posts: 33

Rep: Reputation: 15
Quote:
Originally Posted by PKrishna View Post
hi thanks a lot for the quick reply,
i tried that but it gave me syntax error near unexpected token '<'
Hmm.. looks like it should have worked, well except missing the file to grep that string in. Does this "syslog" file you're using ONLY have IP addresses in it, or does it have more information (like a syslog file normally does)?

You could try doing

Code:
for e in `cat /path/to/syslog`;do grep -w $e /path/to/Inventory;done
 
Old 03-17-2008, 11:43 AM   #5
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Hey jsurles,

its working thanks a ton!!!!!!!!!!!!

no this syslog file tha m talkin abt just has the IP addresses; its d copy of the original syslog file with all other data removed.

thanks again to bigrigdriver and jsurles.

cheers,
Prem
 
Old 03-17-2008, 11:59 AM   #6
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Smile :)

Hey jsurles,

its working thanks a ton!!!!!!!!!!!!

no this syslog file tha m talkin abt just has the IP addresses; its d copy of the original syslog file with all other data removed.

thanks again to bigrigdriver and jsurles.

cheers,
Prem
 
Old 03-17-2008, 12:29 PM   #7
clsgis
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Rep: Reputation: 1
Lightbulb while loop better than for loop

How do i get
172.120.2.6 device6
172.120.2.2 device2
from the following files?


Syslog
172.120.2.6
172.120.2.2

Inventory
172.120.2.1 device1
172.120.2.2 device2
....
172.120.2.9 device9

Your help would be much appritiated.[/QUOTE]


Using the for loop, the entire contents of the Syslog file will appear in the command line. That's awkward for large files. Here's a way that reads one line at a time.


Code:
while read ipaddress
do
  grep "$ipaddress" Inventory
done < Syslog > outputfile
It's more readable, too. The quotes around the first grep argument are there in case there are spaces. Notice both methods are vulnerable to hostile input. If there are semicolons, backtics, parentheses etc in the Syslog file they will be interpreted by the shell. Beware that your file doesn't contain a line like

Code:
4.2.2.1; rm -r /usr/lib
 
Old 03-17-2008, 09:39 PM   #8
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
thanks clsgis
 
  


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
new in scripting bob4linux Programming 10 10-27-2007 05:54 PM
Scripting help please Decorte Linux - Newbie 3 10-21-2007 06:31 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting help please deepsix Linux - General 5 09-07-2005 09:50 PM
Need some scripting help Askari Programming 1 11-26-2003 10:25 AM


All times are GMT -5. The time now is 06:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration