checking entries between files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking entries between files
# 1  
Old 11-18-2007
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  
Old 11-18-2007
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  
Old 11-18-2007
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  
Old 11-18-2007
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  
Old 11-18-2007
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  
Old 11-19-2007
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  
Old 11-20-2007
Excellent help guys ... it works !! Thanks to all who provided help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking for the absence of files

Hi All, I am trying to put a condition to check if two required files are not present in a particular directory and alert accordingly. cd /root F1=sample1 F2=sample2 if ] then echo "One of the files is not present" else echo "Both the files are present" fi Though both F1 and... (7 Replies)
Discussion started by: swasid
7 Replies

2. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

3. Shell Programming and Scripting

Checking dir and files!

Hi Guys, I wrote a code which checks for the text and directory. If the path is incorrect than it should echo that particular path not found. I can create a different if statments but I want to do this in just one if statment with the use of "or" (||). I have succssfully executed this code but now... (2 Replies)
Discussion started by: dixits
2 Replies

4. UNIX for Dummies Questions & Answers

Need help in checking for files in subfolders

Hi, I am trying to print a listing of files from the top level directory, check to see if any files have the same name as the top level directory name and if so, cd to that file and list the files under it. Don't know how to check for the file in the next level. What I have so far: ... (6 Replies)
Discussion started by: tes218
6 Replies

5. Shell Programming and Scripting

checking thorough files and renamingit

hi all am very new to unix scripting..... i have a work right now that i should complete by End of Day :( scenario is i get some x number of files with .csv extension on a specified path at my operating system. the names of the files are as follows . the record lay out is same... (0 Replies)
Discussion started by: rajesh_tns
0 Replies

6. Shell Programming and Scripting

Checking for presence of any files

Is there code in Cshell scripting to check for the presence of any files in the current directory (and only the current directory)? I tried: if (-r *) then ... but Cshell doesn't like that. Thanks, Paul Hudgens (0 Replies)
Discussion started by: phudgens
0 Replies

7. Shell Programming and Scripting

Checking for presence of any files

I have tried the following script to check for the presence of any files in a folder: if (-r *) then goto ZipMiscFiles else echo "" echo " No Miscellaneous files found. Exiting program." echo "" exit endif The -r works fine with the wildcard in combo with other... (4 Replies)
Discussion started by: phudgens
4 Replies

8. Shell Programming and Scripting

checking ERRors in files

I m having trouble in a script.I need To write a script that will check for Following Errors in Logs Files,i.e files having Extension .log The erros are 2008-01-01 15:19:11,822 ERROR - ORA-01115: IO error reading block from file 51 (block # 717090) ORA-01110: data file 51:... (4 Replies)
Discussion started by: ali560045
4 Replies

9. Shell Programming and Scripting

how to login into another ip and checking for the files

Hi All, Good Day. need one help regarding unix script. i have to check whether the particular file is there or not in another ip address. suppose....there is file in this ip address 2.160.64.130 in particualar location. i have to veify this from another ip adress(Say 2.160.64.131).if the file... (2 Replies)
Discussion started by: saikumar_n
2 Replies

10. UNIX for Dummies Questions & Answers

checking for files on ftp...

I have automated my ftp session as given in on of the previous threads as: #! /usr/bin/ksh HOST=remote.host.name USER=whoever PASSWD=whatever exec 4>&1 ftp -nv >&4 2>&4 |& print -p open $HOST print -p user $USER $PASSWD print -p cd directory print -p binary print -p get xyz wait... (3 Replies)
Discussion started by: jithinravi
3 Replies
Login or Register to Ask a Question