I have to join two files based on 1st column where 4th column of a2.txt=at and take 2nd column of a1.txt and 3rd column of a2.txt and check against source files ,if matches list those source file names.
a1.txt
a1|20090809|20090810
a2|20090907|20090908
a2.txt
a1|d|file1.txt|at
a1|d|file2.txt|at
a1|d|file3.txt|st
a2|d|file4.txt|st
a2|d|file5.txt|st
I have source files in my dir
file1_20090809.txt
file2_20090809.txt
file3_20090809.txt
I am expecting o/p like that
file1_20090809.txt
file2_20090809.txt
With my nawk on Solaris 'system' seems to return the status of the 'spawning' of the command, rather than the return status of the command (although the 'man nawk' says otherwise).
I used to check for the file existence with the 'getline':
perl -le'
open F1, $ARGV[0] or die "$ARGV[0]: $!";
%f1 = map { ( split /\|/ )[ 0, 1 ] } <F1>;
open F2, $ARGV[1] or die "$ARGV[1]: $!";
while (<F2>) {
@f2 = split /\|/;
if ( exists $f1{ $f2[0] } && /at$/ ) {
$f2[-2] =~ /(.+)(\.[^.]+)$/;
$fn = $1 . "_" . $f1{ $f2[0] } . $2;
-e $fn and print $fn;
}
}' a1.txt a2.txt
I agree with vgersh99 that it's better to do the test inside awk without spawning an external command,
but I'd like to point out the that approach assumes that the files are readable..
you're right - it works as expected with Solaris' 'nawk' - I did test it correctly.
The thing is that the nawk's "system" invokes sh (Bourne) and 'test -e' doesn't exist for Bourne shell - see 'man test':
-e file
True if file exists. (Not available in sh.)