Tuesday, May 20, 2008

Shell Script Comparing files ...

I have a file (file1) which is like

host1
host2
host3
host4
the list goes on............


Now I want the above lines in files to be compared with files under
/opt/new/*

File names under /opt/new are as below:
Dev
Prod
QA

And suppose host1 from file1 is found under Dev(file under /opt/new)
than it should write under a seperate file New-list as host1-DEV

Ans:

while read line; do echo ${line}-$(grep -l $line /opt/new/*); done < file1 > new-file

The above line reads from file1 and compares the list of hosts given under it with
all files under /opt/new/* and if the host on file matches on any of file.

Then it writes to a new file (new-file) as
host-filename, here host is the one listed in file1 and filename is
the file under which it found the host .

eg if it finds host1 under dev file (/opt/new)

It will write as host1-dev under new-file !