The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
checking ERRors in files ali560045 Shell Programming and Scripting 4 06-19-2008 10:56 AM
how to login into another ip and checking for the files saikumar_n Shell Programming and Scripting 2 07-11-2007 02:34 PM
how to login into another ip and checking for the files saikumar_n UNIX for Advanced & Expert Users 1 07-11-2007 10:13 AM
checking for files on ftp... jithinravi UNIX for Dummies Questions & Answers 3 06-22-2007 11:25 AM
Searching list of entries in file for actual files in dir not4google UNIX for Dummies Questions & Answers 2 10-18-2006 11:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2007
za_7565 za_7565 is offline
Registered User
  
 

Join Date: Nov 2007
Location: ON, Canada
Posts: 16
checking entries between files

I need to write a script for:
I have two files where I need to check entries and sort of compare:
file1:
data01
data02
data03
data04
data05
.
.
.
data81

file2:
/vol/vx/data01
/vol/vx/data02
/vol/vx/data03
/vol/vx/data04
/vol/vx/data05
.
.
So each entry from file1 should have corresponding entry in file2. If entry exists it should pass but if there is no entry it should say:
error: entry dataXXX has no entry in file2.

Appreciate your help.
  #2 (permalink)  
Old 11-18-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
With awk:

Code:
awk '
BEGIN {FS="/"}
FNR==NR {arr[$0]=$0;next}
{arr2[$4]=$4}
END {
  for(i in arr) {
    if (!arr2[i]) {
      print "error: entry " arr[i] " has no entry in file2"
    }
  }
}' "file1" "file2"
Regards
  #3 (permalink)  
Old 11-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
Code:
awk 'NR==FNR{x[$NF];next}
{print ($1 in x)?$1:"error: entry "$1" has no entry in file2"
}' FS="/" file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris.


PS. Just saw the post above,
if you wnat an output like that:

Code:
awk 'NR==FNR{x[$NF];next}
!($1 in x)&&$0="error: entry "$1" has no entry in file2"
' FS="/" file2 file1

Last edited by radoulov; 11-18-2007 at 12:43 PM..
  #4 (permalink)  
Old 11-18-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
Try this:

Code:
cat file1 | while read key
do
  grep "/vol/vx/$key$" file2 2>&1 >/dev/null # Some systems may accept "grep -q".
  [ $? -ne 0 ] && echo "error: entry $key has no entry in file2"
done
  #5 (permalink)  
Old 11-18-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
awk

input:
Code:
a:
data01
data02
data03
data04
data05
b:
/vol/vx/data01
/vol/vx/data02
/vol/vx/data05
output:
Code:
No entry for:data03
No entry for:data04
code:
Code:
nawk 'BEGIN{FS="/"}
{
if (NR==FNR) 
	test[NR]=$4
else
{
	flag=0
	for (i in test)
	{
		if ($1==test[i]) 
			flag=1
	}
	if (flag==0)
		print "No entry for:"$1
}
}' b a
  #6 (permalink)  
Old 11-19-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
Hi.

It's useful to see different approaches. I like the awk solutions. The shell loop solution is also good, but will call grep for each line, possibly a drawback for long files.

Here is a solution using *nix commands, although a modern shell is required for the process substitution, "<( ... )". This may be useful if it is not appropriate or possible to use awk, perl, etc.:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate feature.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sort cut comm sed

echo

echo " Input file data1:"
cat data1

echo
echo " Input file data2:"
cat data2

echo
echo " Results:"
comm -23 <( sort data1 ) <( cut -d/ -f4 data2 | sort ) |
sed -e 's/^/ No entry for: /'

exit 0
Produces, using summer_cherry's datasets:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
sort (coreutils) 5.2.1
cut (coreutils) 5.2.1
comm (coreutils) 5.2.1
GNU sed version 4.1.2

 Input file data1:
data01
data02
data03
data04
data05

 Input file data2:
/vol/vx/data01
/vol/vx/data02
/vol/vx/data05

 Results:
 No entry for: data03
 No entry for: data04
See man pages for details ... cheers, drl
  #7 (permalink)  
Old 11-20-2007
za_7565 za_7565 is offline
Registered User
  
 

Join Date: Nov 2007
Location: ON, Canada
Posts: 16
Excellent help guys ... it works !! Thanks to all who provided help.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0