merging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging
# 1  
Old 07-30-2010
merging

Hi all,


I have 2 files. I want to merge a portion or column in file 2 into file 1.

file 1 - not tab or space delimited

Code:
B_1       gihgjfhdj| hgfkddlldjljldjlddl
B_2       gihgjddshjgfhs| hgfkddlldjljldjlddl
B_3       gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj


file2 - tab delimited

Code:
asdfg     1101    2579    +    492    229194403    0    -    be_10    -
D-asd     2669    4000    +    443    229194404    0    -    be_20    -
Perd    4162    5049    +    295    229194405    0    -    be_30    -

I need the output file where the 9th column or tab on file 2 is merged to file 1
output file (modified file1)

Code:
B_1       gihgjfhdj| hgfkddlldjljldjlddl  be_10
B_2       gihgjddshjgfhs| hgfkddlldjljldjlddl be_20
B_3       gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj be_20

Please let me know the best way to do in unix or awk or sed.

LA
# 2  
Old 07-30-2010
In sh...
Code:
#!/bin/sh

cut -f9 f2 > tmpfile
paste f1 tmpfile

exit 0

f1 and f2 are respectively your first and second files
Waiting for an awk solution Smilie
# 3  
Old 07-30-2010
Hi,

or with awk:

Code:
awk 'NR==FNR{a[++i]=$9}NR!=FNR{t=$1;sub(/B_/,"",t);print $0 FS a[t]}' file2 file1

Output:
Code:
B_1       gihgjfhdj| hgfkddlldjljldjlddl be_10
B_2       gihgjddshjgfhs| hgfkddlldjljldjlddl be_20
B_3       gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj be_30

HTH Chris
# 4  
Old 07-30-2010
Thanks Chris,
It worked for that particular files. I guess u were targeting /B_/. but I have many files that have different ids. So is there a way to make it a generalized code so that whatever the id is, the code works for any data with the same structure and format.

let me know.
LA
# 5  
Old 07-30-2010
Yupp,

try this:
Code:
awk '{getline s < "file2"; split(s,a,"[ \t]+");print $0 FS a[9]}' file1

# 6  
Old 07-30-2010
It didn't work
# 7  
Old 07-31-2010
For me it does work. So what exactly doesn't work for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on merging

I have a total of 100 files (variable size of each file) with total size of 328950 bytes. I want to merge those 100 files into 4 files with each size be close to equal size i.e (328950/4 ~= 82238) but do not want to break any file. Any unix sheel script help will be really helpful. (18 Replies)
Discussion started by: George1234
18 Replies

2. Shell Programming and Scripting

Interval merging

I do have a file (file1) with a specified interval of 500 counts file1 0 500 500 1000 1000 1500 1500 2000 2000 2500 2500 3000 3000 3500 3500 4000 4000 4500 4500 5000 5000 5500 5500 6000 6000 6500 6500 7000 7000 7500 7500 8000 (3 Replies)
Discussion started by: Kanja
3 Replies

3. Shell Programming and Scripting

Merging 2 Arrays

I am trying to create a script that combines 2 arrays: #!/bin/bash read -a unix #(a c e g) read -a test #(b d f) #now I want to merge ${unix} with ${test}, one after another such that the result would be: (abcdefg) #I've tried quite a few options and can't seem to make it work (5 Replies)
Discussion started by: pbmitch
5 Replies

4. Shell Programming and Scripting

Merging

Hi, I have searched the forums for a solution but I haven't found a perfect answer, and I'm a bit of a novice, so I hope someone can help: I have 2 files: file1: Chr1 139311 1/1:37,3,0:19 Chr1 139350 1/1:67,6,0:19 Chr1 139404 1/1:0,0,0:7 Chr1 152655 0/1:0,0,0:3 Chr1 152718... (2 Replies)
Discussion started by: ljk
2 Replies

5. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

6. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

7. UNIX for Dummies Questions & Answers

Merging files

Hi i have two files say file 1 contents are A B C D E I have file2 contents are B E F G C K I want to have new file like A B (4 Replies)
Discussion started by: ssuresh1999
4 Replies

8. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies

9. Shell Programming and Scripting

Merging arrays

Hi all, I need some help in merging arrays. I have two arrays and using korn shell Array1 AB23 AB24 Array2 CD00 CD01 CD02 Elements from array 1 should always alternate with elements of arrays 2 i.e the result should look like AB23CD00 AB24CD01 AB23CD02 Any help is appreciated.... (4 Replies)
Discussion started by: jakSun8
4 Replies

10. Shell Programming and Scripting

Merging Help

Hi Gurus, I need a help in merging the files. I have nearly 7 files and the files will have time stamp in it. I need to merger these files condition is it is not necessary that all the 7 files has to be there. suppose if i have only 3 files availabe out of these 7 then i need to merge... (3 Replies)
Discussion started by: kumarc
3 Replies
Login or Register to Ask a Question