LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I am new to scripting (https://www.linuxquestions.org/questions/linux-newbie-8/i-am-new-to-scripting-628689/)

PKrishna 03-17-2008 10:31 AM

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.

bigrigdriver 03-17-2008 10:46 AM

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

PKrishna 03-17-2008 10:58 AM

hi thanks a lot for the quick reply,
i tried that but it gave me syntax error near unexpected token '<' :(

jsurles 03-17-2008 11:34 AM

Quote:

Originally Posted by PKrishna (Post 3091526)
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

PKrishna 03-17-2008 11:43 AM

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

PKrishna 03-17-2008 11:59 AM

:)
 
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

clsgis 03-17-2008 12:29 PM

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

PKrishna 03-17-2008 09:39 PM

thanks clsgis :)


All times are GMT -5. The time now is 03:39 PM.