Substring and Join


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substring and Join
# 1  
Old 03-21-2011
Substring and Join

Experts,

Im struggling with something for the past hour or so and here is the challenge.

File1:
DO93948388 LDCNND 343 48848 -- Row1
DKKDF9933433 DKK 3384774 DLLFLFD -- Row2
DKKFJ38383734 DJF934988 REPFD FD -- Row3


File2:
LDCNNDDKJF --- Row1
DNCNDNCND --- Row2
UFUUOP383 --- Row3
DLLFLFDKDKF63 --- Row4
DJF9349883477 --- Row5


File3(Output):
DO93948388 LDCNND LDCNNDDKJF
DKKDF9933433 DLLFLFD DLLFLFDKDKF63
DKKFJ38383734 DJF934988 DJF9349883477

In the above scenario, in File1, either Col2 or Col3 or Col4 might be a substring in File2's Column1 and if it is, then please fetch the matching Row from File 1 & File2 as shown above in File3.

In sql, we would probably code the above as something like this ...
instr( file2.col1, file1.col2) > 0 or
instr(file2.col1, file1.col3 ) > 0 or
instr(file2.col1, file1.col4) > 0.

Im not sure as how to go about this in unix.

Please help.
Lee.

Im not sure as how to go about this as
# 2  
Old 03-21-2011
Code:
cat file1
DO93948388 LDCNND 343 48848
DKKDF9933433 DKK 3384774 DLLFLFD
DKKFJ38383734 DJF934988 REPFD FD

cat file2
LDCNNDDKJF
DNCNDNCND
UFUUOP383
DLLFLFDKDKF63
DJF9349883477

 
awk 'NR==FNR{a[NR]=$1;p=NR;next}{for(i=2;i<=NF;i++) {for(j=1;j<=p;j++) {if (sub($i,$i,a[j])) {print $1 FS $i FS a[j]}}}}' file2 file1
DO93948388 LDCNND LDCNNDDKJF
DKKDF9933433 DLLFLFD DLLFLFDKDKF63
DKKFJ38383734 DJF934988 DJF9349883477
DKKFJ38383734 FD DLLFLFDKDKF63

# 3  
Old 03-21-2011
How about this:

Code:
awk 'NR==FNR{S[$0];next} {for(s in S) for(i=2;i<=NF;i++) if(s ~ "^"$i) print $1" "$i" "s; }' file2 file1

# 4  
Old 03-21-2011
Thanks Chuber_XL, your code is much better than mine, I learn a lot from you Smilie
# 5  
Old 03-21-2011
Yinyuemi & Chubler_XL,

Absolutely brilliant !!! I envy you folks.

Thanks a million.

Another request, lets say if I want to see just what is only in File2 or File1, how do I get that ?

Lee.
# 6  
Old 03-21-2011
I this what your after?

Code:
awk 'NR==FNR{S[$0];next}
{for(s in S) for(i=2;i<=NF;i++) if($i&&s ~ "^"$i) {$i=x;S[s]="X"}
for(i=2;i<=NF;i++) if($i)print $i} END {for(s in S) if(S[s]!="X") print s}' file2 file1
343
48848
DKK
3384774
REPFD
FD
DNCNDNCND
UFUUOP383

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

3. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

4. Shell Programming and Scripting

Matching the substring and join two files

Hi I had two files like below. file-1 101001234567890 101001234567891 101001234567892 101001234567893 101001234567894 101001234567895 101001234567896 101001234567897 101001234567898 101001234567899 file-2 (6 Replies)
Discussion started by: p_sai_ias
6 Replies

5. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

6. Shell Programming and Scripting

substring

I have a string '<Hi>abc</Hi>" How to print "abc" (6 Replies)
Discussion started by: sandy1028
6 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

9. Shell Programming and Scripting

join (pls help on join command)

Hi, I am a new learner of join command. Some result really make me confused. Please kindly help me. input: file1: LEO oracle engineer 210375 P.Jones Office Runner ID897 L.Clip Personl Chief ID982 S.Round UNIX admin ID6 file2: Dept2C ID897 6 years Dept5Z ID982 1 year Dept3S ID6 2... (1 Reply)
Discussion started by: summer_cherry
1 Replies

10. Shell Programming and Scripting

getting a substring

Hi, I have several lines like this ones: 123456789abcde /aa/bb/123456_$data.log 123456789abcde /aa/bb/123456_not_a_data_log 987654321ab /aa/bb/xpto123456_$data.log ... How do I get into a variable the value "/aa/bb/123456_$data.log" , searching only for the beggining and ending... (3 Replies)
Discussion started by: Scarlos
3 Replies
Login or Register to Ask a Question