Home Man
Search
Today's Posts
Register

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

 
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.
The Following User Says Thank You to RudiC For This Useful 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 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
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)
# 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.
The Following User Says Thank You to Don Cragun For This Useful 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 01:05 PM..
The Following User Says Thank You to RudiC For This Useful Post:
arunkumar_mca (10-03-2017)
Login to Reply

« Previous Thread | Next Thread »
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 XML file and print the values in the text file using Linux shell script sravanreddy UNIX for Dummies Questions & Answers 1 07-22-2015 11:26 AM
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 08:58 PM
Replace text in column1 of a file matching columns of another file thienxho Shell Programming and Scripting 2 12-11-2012 05: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 03:57 PM
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 09:21 AM.

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