Search the key from a file to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search the key from a file to another file
# 1  
Old 11-29-2016
Search the key from a file to another file

Hi,

I need help in solving the below need in UNIX.

I have a file datafile as below

Code:
pg1,dvn1,scls1,cls1,itp1,sku1
pg2,dvn2,scls2,cls2,itp2,sku2

and keyfile as below


Code:
scls1,1000,a,b,c,d,p1
scls2,1001,a,b,c,d,p1
scls1,1000,a1,b,c,d,p1
scls2,2000,a,b,c,d,p1


I need to search first record of first line keyfile i.e. in the first line of datafile
if match found write the pg1 in a new variable
use this variable value to perform some action
this needs to be done till the last line in keyfile

Request you to please help me in doing this.

Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 11-29-2016 at 11:40 AM.. Reason: Changed CODE tags.
# 2  
Old 11-29-2016
Not clear. Please be way more descriptive, supplying desired output samples.

And: Any attempts/ideas/thoughts from your side?
# 3  
Old 11-29-2016
Hi,

Thanks for the reply.
Desired o/p is as below

Code:
pg1,scls1
pg2,scls2

I was trying something like below
awk 'NR==FNR{a[$1] keyfile datafile

But not getting what to do next

Last edited by RudiC; 11-29-2016 at 12:00 PM.. Reason: Changed ICODE to CODE tags.
# 4  
Old 11-29-2016
And this line by line, i.e. a match of line 3 with line 5 should be discarded?
# 5  
Old 11-29-2016
Sorry let me modify the requirement in the below way

let's say if my keyfile is as below

Code:
scls1,1000,a,b,c,d,p1
scls2,1001,a,b,c,d,p1
scls1,1000,a1,b1,c,d,p1
scls2,2000,a1,b,c,d,p1

datafile

Code:
pg1,dvn1,scls1,cls1,itp1,sku1
pg2,dvn2,scls2,cls2,itp2,sku2
pg3,dvn3,scls1,cls3,itp3,sku3
pg4,dvn4,scls4,cls4,itp4,sku4
pg5,dvn5,scls2,cls5,itp5,sku5

I will will search first field of first line in keyfile into all the lines in datafile
if in data file I find ,write in a file
if not then go to the second line of keyfile and again search into all the lines in datafile

this process will loop till the last line of keyfile

Now in a code where I will find the above I will store the first field in a variable as below
Code:
var1=pg1
<<do some work>>
again var1=pg3
<<do some work>>
--it stops here because scls1 has pg1 and pg3 match in datafile 
-- again it loops for scls2 from keyfile into datafile which gives as below
var1=pg2
<<do some work>>
again var1=pg5
-- this process will continue till last line in keyfile
-- scls1 comes in 1 and 3rd line but needs to be assigned to var1 in all occurrence

Thanks in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for specific key in a file and print it

Hi All, I have a file abc.txt Database unique name: NEPAL Database name: NEPAL Services: NEPAL_COB,NEPAL_PROD output i need is it should search "Services" and it that search "COB" word so output i need is like NEPAL_COB (3 Replies)
Discussion started by: amar1208
3 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

How to extract information a file according key id in another file?

hi, i have a large file containing the detailed information of a bunch of keys like this: JAT_0001 contig102_342_3_n2 contig102_342 atgcacgacta 30 50 20... (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. Shell Programming and Scripting

Retrieving the relevant search from search file in the main file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search... (7 Replies)
Discussion started by: csim_mohan
7 Replies

5. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

6. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

7. Shell Programming and Scripting

Perl - start search by using search button or by pressing the enter key

#Build label and text box $main->Label( -text => "Input string below:" )->pack(); $main->Entry( -textvariable => \$text456 )->pack(); $main->Button( -text => "Search", -command => sub { errchk ($text456) ... (4 Replies)
Discussion started by: popeye
4 Replies

8. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

9. Shell Programming and Scripting

Search Key in same file but at different positions

Hi All, I want some help in unix. Current code is for DOC in `InputFileName | awk '{ if ( substr($0, 392, 1)=="C") { $0 = (substr($0, 28, 15) "" substr($0, 386, 3)) } else { ($0 = substr($0, 28, 18))}; print }' | sort | uniq` do grep ${DOC} InputFileName > DOCKET_${DOC}.txt done ... (18 Replies)
Discussion started by: kam786sim
18 Replies

10. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies
Login or Register to Ask a Question