UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Reading file and matching it from set of file

👤 Login to reply

    #1  
Old 10-03-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
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.
Sponsored Links
    #2  
Old 10-03-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Some samples of matching and non-matching data lines in the files, please.
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-03-2017)
Sponsored Links
    #3  
Old 10-03-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
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 11:53 AM..
The Following User Says Thank You to Corona688 For This Useful Post:
arunkumar_mca (10-03-2017)
    #4  
Old 10-03-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
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.
The Following User Says Thank You to arunkumar_mca For This Useful Post:
Corona688 (10-03-2017)
Sponsored Links
    #5  
Old 10-03-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
arunkumar_mca (10-03-2017)
Sponsored Links
    #6  
Old 10-03-2017
arunkumar_mca arunkumar_mca is offline
Registered User
 
Thanks that worked. Thanks a lot
Sponsored Links
    #7  
Old 10-03-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
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 01:05 PM..
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-03-2017)
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find matching file in bash with variable file names but consisent prefixs cmccabe Shell Programming and Scripting 5 10-19-2016 09: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 05:45 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 04:15 AM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 04: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 04:25 AM



All times are GMT -4. The time now is 07:02 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?