Unix/Linux Go Back    


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

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
arunkumar_mca's Unix or Linux Image
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 11 December 2017, 10:01 AM EST
Posts: 395
Thanks: 61
Thanked 2 Times in 2 Posts
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 Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 2:47 PM EST
Location: Aachen, Germany
Posts: 11,983
Thanks: 356
Thanked 3,693 Times in 3,391 Posts
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 Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
Corona688's Unix or Linux Image
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 January 2018, 2:54 PM EST
Location: Saskatchewan
Posts: 22,574
Thanks: 1,164
Thanked 4,293 Times in 3,961 Posts
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..
The Following User Says Thank You to Corona688 For This Useful Post:
arunkumar_mca (10-03-2017)
    #4  
Old Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
arunkumar_mca's Unix or Linux Image
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 11 December 2017, 10:01 AM EST
Posts: 395
Thanks: 61
Thanked 2 Times in 2 Posts
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 Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 8:59 AM EST
Location: San Jose, CA, USA
Posts: 10,946
Thanks: 611
Thanked 3,824 Times in 3,268 Posts
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 Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
arunkumar_mca's Unix or Linux Image
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 11 December 2017, 10:01 AM EST
Posts: 395
Thanks: 61
Thanked 2 Times in 2 Posts
Thanks that worked. Thanks a lot
Sponsored Links
    #7  
Old Unix and Linux 10-03-2017   -   Original Discussion by arunkumar_mca
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 2:47 PM EST
Location: Aachen, Germany
Posts: 11,983
Thanks: 356
Thanked 3,693 Times in 3,391 Posts
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..
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-03-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 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 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



All times are GMT -4. The time now is 04:11 PM.