- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Help in script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 11:19 AM
тАО05-29-2009 11:19 AM
Help in script
Can some one please help me in writing a script to accomplish that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 11:30 AM
тАО05-29-2009 11:30 AM
Re: Help in script
> /tmp/different_files.txt #clean output file
for f in `cat file1`
do
grep -q "$f" file2
r=$?
if [ $r -ne 0 ]
then
echo $f >> /tmp/different_files.txt
fi
done
for f in `cat file2`
do
grep -q "$f" file1
r=$?
if [ $r -ne 0 ]
then
echo $f >> /tmp/different_files.txt
fi
done
This is not the most elaborate way of doing what you want but most explicit way so that you can follow and understand, then if necessary, modify it. Ask if you have any questions
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 11:35 AM
тАО05-29-2009 11:35 AM
Re: Help in script
If you can sort (or don't mind sorting) the two files, then you can use 'comm' to make your comparisons.
If you want to compare the contents of two directories, you can use 'diff'.
See the manpages for these commands for more information.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 12:31 PM
тАО05-29-2009 12:31 PM
Re: Help in script
lets assume you have two files contains file names
firstfile
---------
abc
def
fgi
abc
secondfile
---------
abc
def
ghi
abc
#!/usr/bin/sh
>present_in_second_not_in_first
>present_in_first_not_in_second
>common
cat firstfile |sort|uniq >firtfile_last
cat secondfile |sort |uniq >secondfile_last
cat firstfile_last |while read line
do
grep -x "line" secondfile_last |read dummy # you can remove -x option as it will look for exact line matching
if [ "$?" = "0" ] ;then
echo $line >>present_in_first_not_in_second
else
echo $line >>common
fi
done
cat secondfile_last |while read line
do
grep -x "line" firstfile_last |read dummy
if [ "$?" != "0" ] ; then
echo $line >>present_in_second_not_in_first
fi
done
it will give you three files
common --- common entries in both
present_in_second_not_in_first---- present in second file but not in first file
present_in_first_not_in_second---- present in first file but not in second file
Note:tested ..feel free to tell me if it is not working
Rgds
Johnson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 12:36 PM
тАО05-29-2009 12:36 PM
Re: Help in script
#!/usr/bin/sh
>present_in_second_not_in_first
>present_in_first_not_in_second
>common
cat firstfile |sort|uniq >firstfile_last
cat secondfile |sort |uniq >secondfile_last
cat firstfile_last |while read line
do
grep -x "line" secondfile_last |read dummy # you can remove -x option as it will look for exact line matching
if [ "$?" = "0" ] ;then
echo $line >>common
else
echo $line >>present_in_first_not_in_second
fi
done
cat secondfile_last |while read line
do
grep -x "line" firstfile_last |read dummy
if [ "$?" != "0" ] ; then
echo $line >>present_in_second_not_in_first
fi
done
Rgds
Johnson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2009 01:29 AM
тАО05-30-2009 01:29 AM
Re: Help in script
Unfortunately this is the worst way performance wise, it doesn't use vector methods and is N squared. (I won't mention the evil cat and ``. ;-)
As JRF said, sort both files then use comm:
sort file1 > file1.sort
sort file2 > file2.sort
comm -3 file1.sort file2.sort
>Johnson: lets assume you have two files contains file names
Directories don't have duplicates so you don't need uniq(1). (And sort has -u.)
>present_in_second_not_in_first
>present_in_first_not_in_second
>common
To get these you simply do the following with my files above:
comm -13 file1.sort file2.sort > present_in_second_not_in_first
comm -23 file1.sort file2.sort > present_in_first_not_in_second
comm -12 file1.sort file2.sort > common