Append file from ref file AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append file from ref file AWK
# 1  
Old 06-13-2011
Append file from ref file AWK

FILE1

Code:
abc:xxx:abc:123:wer:AAA:12
csf:xxx:123:aeg:sar:BBB:13
asq:yer:321:wsa:qqq:CCC:14

FILE2
Code:
 
AAA:12:SET1:R1
AAA:12:SSS1:RR1
AAA:11:SET4:R3
BBB:13:SET2:R2

OUTPUT
Code:
 
abc:xxx:abc:123:wer:AAA:12:SET1:R1:SSS1:RR1
csf:xxx:123:aeg:sar:BBB:13:SET2:R2::
asq:yer:321:wsa:qqq:CCC:14::::

If $1:$2 from FILE2 exactly matches with $6:$7 fields of FILE1 ,append $3:$4 from FILE2 as $8:$9 in the OUTPUT ,if FILE2 has multiple matching $1:$2 keep appending , if no match then print line from FILE1 as it is

Thanks !
# 2  
Old 06-13-2011
Hello greycells,

Sorry but there a lot of similar excerices asked and replied in the past. Did you search the forum?

I believe a start from you will help you to understand the things very well instead of we posting the solution you need.

Regards
Ravi
These 2 Users Gave Thanks to panyam For This Post:
# 3  
Old 06-13-2011
I did search but could not find anything which would work with all my conditions ... but let me post what i tried . thx
# 4  
Old 06-13-2011
Of course you're never going to find anything that does exactly what you want, what're the odds of finding someone with the exact same problem, but you may find material that's easily adapted.
# 5  
Old 06-13-2011
Code:
awk -F ":" 'NR==FNR{a[$1 FS $2]=(a[$1 FS $2]=="")?$3 FS $4:a[$1 FS $2] FS $3 FS $4;next}
            {print $0 (a[$6 FS $7]==""?X: FS a[$6 FS $7])}' file2 file1

abc:xxx:abc:123:wer:AAA:12:SET1:R1:SSS1:RR1
csf:xxx:123:aeg:sar:BBB:13:SET2:R2
asq:yer:321:wsa:qqq:CCC:14

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Advanced & Expert Users

Map snps into a ref gene file

I have the following data set about the snps ID txt file POS ID 78599583 rs987435 33395779 rs345783 189807684 rs955894 33907909 rs6088791 75664046 rs11180435 218890658 rs17571465 127630276 rs17011450 90919465 rs6919430 and a gene... (7 Replies)
Discussion started by: marwah
7 Replies

3. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

4. Shell Programming and Scripting

awk dynamically append columns into file

Hi, I have a large data file, want to separate it into 100 part and export one specific field as a file; then I want to append each part's column into one file. How to realize that? 1 2 3 1 2 3 4 2 2 3 4 3 3 3 4 4 3 4 5 5 3 4 5 6 I want the last column of the data file, e.g divide it... (5 Replies)
Discussion started by: wanliushao
5 Replies

5. Shell Programming and Scripting

awk to append data to a file

Hi I search for certain values in a file across many directories using the following awk code awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print Sl, Tt}' DILAT.DAT What I would like to do is write out Sl and Tt obtained from these files from many directories to a single file. So for example if... (2 Replies)
Discussion started by: lost.identity
2 Replies

6. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

7. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below based on the common elements from the first coulmns of 1.txt and 2.txt. 1.txt 11 12 13 14 15 16 17 18 19 20 21 2.txt (6 Replies)
Discussion started by: p_sai_ias
6 Replies

8. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below (using awk) 1.txt 11 a1 12 a2 13 a3 14 a4 15 a5 16 a6 17 a7 18 a8 19 a9 20 a10 2.txt 14 b1 15 b2 16 b3 (8 Replies)
Discussion started by: p_sai_ias
8 Replies

9. UNIX for Dummies Questions & Answers

Using awk (or whatever) to pull and append data in a new file

One of the fortunate things about posting in a "Dummies" forum is you probably aren't expecting a lot of out me... I'm trying to pull fields from two lines in the same file(s), and then append them together in a new file. So...I get a nice line-by-line of the first bit of data I'm looking... (6 Replies)
Discussion started by: Milano_EH3
6 Replies

10. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies
Login or Register to Ask a Question