The UNIX and Linux Forums  


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 11:56 AM
how to login into another ip and checking for the files saikumar_n Shell Programming and Scripting 2 07-11-2007 03:34 PM
how to login into another ip and checking for the files saikumar_n UNIX for Advanced & Expert Users 1 07-11-2007 11:13 AM
checking for files on ftp... jithinravi UNIX for Dummies Questions & Answers 3 06-22-2007 12:25 PM
Searching list of entries in file for actual files in dir not4google UNIX for Dummies Questions & Answers 2 10-18-2006 12:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
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,322
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 12-19-2007
amitrajvarma amitrajvarma is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 43
Quote:
Originally Posted by Franklin52 View Post
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
Hi ,
I am getting the following error,
"Unmatched '.
when I executed the

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"

Please let me know why so. I have a tcsh shell

Thanks

Amit
  #4 (permalink)  
Old 12-19-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

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

Hi,

I tried this one and it works.

input:
Code:
a:
data01
data02
data03
data04
data05

b:
/vol/vx/data03
/vol/vx/data01
output:
Code:
error: entry data04 has no entry in file2.
error: entry data05 has no entry in file2.
error: entry data02 has no entry in file2.

code:
Code:
sed 's/\// /g' b > b.tmp
nawk '
NR==FNR {a[$1]=$1}
NR!=FNR {a[$3]=$0}
END{
for (i in a)
	if (i==a[i])
		print "error: entry "i" has no entry in file2."
}
' a b.tmp
rm b.tmp
  #5 (permalink)  
Old 11-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,910
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..
  #6 (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
  #7 (permalink)  
Old 11-18-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
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
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 01:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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