I have file with set of records. I have to find if the record is available in the set if files and if yes need to get the file names of it.
Example
head unmatch
02000417
02001855
02004639
02005420
02005440
02005454
02006624
02006743
02007097
02008770
-rw-rw-r-- 1 ftpuser pfus 131868931 Oct 3 10:45 50.h
-rw-rw-r-- 1 ftpuser pfus 135538052 Oct 3 10:46 51.h
-rw-rw-r-- 1 ftpuser pfus 123051798 Oct 3 10:47 52.h
-rw-rw-r-- 1 ftpuser pfus 123583711 Oct 3 10:48 01.h
The record in unmatch can be in any of the file 50.h,51.h,52.h,01.h . The record search should happen only on 1-8 position. I have to take the record from unmatch and then compare with all 1-8 position record in 50.h,51.h,52.h,01.h and the print the result where ever it matched.
I think I understand what you're getting at... Each line of unmatch is the first 8 characters of a record you want to retrieve.
awk 'NR==FNR { A[$1] ; next } # Store matching record IDs in A
substr($0,0,8) in A # Check first 8 chars of record and print if in A
' unmatch 50.h 51.h 52.h 01.h
Yes each line of unmatched will be in any of the set of the file with *.h . I have to take a record from unmatch and find if they are available on 1,8 position on *.h and print the record and the file *.h name where it was found.
I executed the awk . it is giving me the record. How can I get the file name too.
substr($0,0,8) in A # Check first 8 chars of record and print if in A
in Corona688's suggestion to:
substr($0,0,8) in A { print FILENAME, $0 } # Check first 8 chars of record and print if in A
assuming that you want the file name printed before the contents of the line.
Showing us sample input and the output you wanted from that sample input (and telling us what operating system and shell you're using) would have gotten you a working suggestion much sooner.
FWIW I could not find one awk that showed a difference, including all awk's on Solaris, AIX and HPUX. Even my version of (mawk 1.3.4 20100625) worked well.
On CentOS it worked fine as well mawk 1.3.4 20161120.
20090726
[..]
+ modify workaround for (incorrect) scripts which use a zero-parameter
for substr to ensure the overall length of the result stays the same.
For example, from makewhatis:
filename_no_gz = substr(filename, 0, RSTART - 1);