Reading file and matching it from set of file

 
Thread Tools Search this Thread
# 1  
Old 10-03-2017
Reading file and matching it from set of file

Hi All,

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
Code:
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.
# 2  
Old 10-03-2017
Some samples of matching and non-matching data lines in the files, please.
This User Gave Thanks to RudiC For This Post:
arunkumar_mca (10-03-2017)
# 3  
Old 10-03-2017
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.

Code:
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

Use nawk on solaris.

Last edited by Corona688; 10-03-2017 at 12:53 PM..
This User Gave Thanks to Corona688 For This Post:
arunkumar_mca (10-03-2017)
# 4  
Old 10-03-2017
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.
This User Gave Thanks to arunkumar_mca For This Post:
Corona688 (10-03-2017)
# 5  
Old 10-03-2017
Change the line:
Code:
substr($0,0,8) in A # Check first 8 chars of record and print if in A

in Corona688's suggestion to:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
arunkumar_mca (10-03-2017)
# 6  
Old 10-03-2017
Thanks that worked. Thanks a lot
# 7  
Old 10-03-2017
Shouldn't that be substr($0,1,8) in A?

I wasn't sure if the substring to be compared had to be in chars 1 - 8 or start in 1 - 8 , i.e. possibly be from 8 - 15.

Try also
Code:
sed 's/^/^/' unmatch | grep -f- *.h


Last edited by RudiC; 10-03-2017 at 02:05 PM..
This User Gave Thanks to RudiC For This Post:
arunkumar_mca (10-03-2017)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
ksh Script, Reading A File, Grepping A File Contents In Another File Brusimm Shell Programming and Scripting 3 11-28-2018 07:36 PM
awk to update file with sum of matching fields in another file cmccabe Shell Programming and Scripting 2 02-22-2018 08:40 AM
Find matching file in bash with variable file names but consisent prefixs cmccabe Shell Programming and Scripting 5 10-19-2016 10:06 AM
Shell script to filter records in a zip file that contains matching columns from another file anil.v Shell Programming and Scripting 1 12-16-2015 06:45 AM
Reading Xml file and print the values into the text file in columnwise? sravanreddy UNIX for Dummies Questions & Answers 4 07-23-2015 04:13 AM
Reading XML file and print the values in the text file using Linux shell script sravanreddy UNIX for Dummies Questions & Answers 1 07-22-2015 12:26 PM
How to append portion of a file content to another file when a certain pattern is matching? bananamen UNIX for Dummies Questions & Answers 25 06-23-2013 09:58 PM
Replace text in column1 of a file matching columns of another file thienxho Shell Programming and Scripting 2 12-11-2012 06:42 PM
awk - writing matching pattern to a new file and deleting it from the current file goddevil Shell Programming and Scripting 2 12-01-2012 04:57 PM
Want to read data from a file name.txt and search it in another file and then matching... ektubbe Shell Programming and Scripting 15 02-15-2012 01:20 PM
Searching for Log / Bad file and Reading and writing to a flat file mlpathir Shell Programming and Scripting 3 10-03-2010 07:01 PM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 05:15 AM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 05:38 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM