Match & append the files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match & append the files
# 1  
Old 12-14-2005
Match & append the files

Hi All,

I have a problem in appending the files

File 1
0.0000001 0.500000039894
0.0000002 0.500000079788
0.0000003 0.500000119683
0.0000004 0.500000159577
0.0000005 0.500000199471
0.0000006 0.500000239365
0.0000007 0.500000279260
0.0000008 0.500000319154
0.0000009 0.500000359048
0.0000010 0.500000398942

File 2
0.0000001 -5.199337582193
0.0000002 -5.068957749718
0.0000003 -4.991217139908
0.0000004 -4.935367444719
0.0000005 -4.891638475699

The output should be something like

0.0000001 0.500000039894 -5.199337582193
0.0000002 0.500000079788 -5.068957749718
0.0000003 0.500000119683 -4.991217139908
0.0000004 0.500000159577 -4.935367444719
0.0000005 0.500000199471 -4.891638475699
0.0000006 0.500000239365
0.0000007 0.500000279260
0.0000008 0.500000319154
0.0000009 0.500000359048
0.0000010 0.500000398942

I searched the forum and was able to figure that it can be done using the following awk command.

nawk -f app.awk File2 File1

Code for app.awk:

BEGIN { FS=OFS=" " }
FNR == NR { arr[$1]=$2; next }
{ $NF=($1 in arr) ? arr[$1] : " "; print }

But still the output is not as expected. I am getting the output as

0.0000001 -5.199337582193
0.0000002 -5.068957749718
0.0000003 -4.991217139908
0.0000004 -4.935367444719
0.0000005 -4.891638475699
0.0000006
0.0000007
0.0000008
0.0000009
0.0000010

Please help me...

Thanks
Shash
# 2  
Old 12-14-2005
Code:
FNR == NR { arr[$1]=$2; next }
{ $(NF+1)=($1 in arr) ? arr[$1] : " "; print }

# 3  
Old 12-14-2005
Thanks a lot vgersh99!. It worked. I think when I searched the forum it was your answer which I took at the first time. You are a genius.

Thanks
Shash
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching two fields in two csv files, create new file and append match

I am trying to parse two csv files and make a match in one column then print the entire file to a new file and append an additional column that gives description from the match to the new file. If a match is not made, I would like to add "NA" to the end of the file Command that Ive been using... (6 Replies)
Discussion started by: dis0wned
6 Replies

2. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

3. Shell Programming and Scripting

Match and append - awk

ALL, Please help with this ... File1 000433,ds00d11,tdev,ds00d11_view,0CD3 000433,ds00d12_34,tdev,ds00d12_view,132D File2 CG01_ds00d11_drs,rs1_ds00d11_0CD3_114D,000433,0CD3 CG01_ds00d11_drs,rs1_ds00d11_0CD3_114D,000566,114D ... (6 Replies)
Discussion started by: greycells
6 Replies

4. Shell Programming and Scripting

Match and Append Based on file contexts

Not Sure how to do this. Some combo of awk and sed perhaps. If String in File1 match String in file2 then append file2 File1.txt BullTerrier Boxer Bulldog File2.txt <Defined info="AllAnimals" group="Adoptions" setting="animals"> <SomeID ="NumbersRepresentingDogName"> <for> <add... (2 Replies)
Discussion started by: TY718
2 Replies

5. Shell Programming and Scripting

Match exact and append zero

file 11 2 12 6 13 7 114 6 011 7 if I'm searching for 11, output needed is output: 11 2 011 7 Code: awk '$1 ~ /^11$/' file I used the above to match exact, but it avoiding "011 7" line too, how to resolve this? (6 Replies)
Discussion started by: Roozo
6 Replies

6. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

7. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

8. UNIX for Dummies Questions & Answers

Match values from 2 files and append certain fields

Hi, I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly. ... (1 Reply)
Discussion started by: redse171
1 Replies

9. Shell Programming and Scripting

Find, Append, Move & Rename Multiple Files

Using a bash script, I need to find all files in a folder "except" the newest file. Then I need to insert the contents of one text file into all the files found. This text needs to be placed at the beginning of each file and needs a blank line between it and the current contents of the file. Then I... (5 Replies)
Discussion started by: Trapper
5 Replies

10. Shell Programming and Scripting

sort & match multiple files

Hi, I have some question and need some guidance how to sort and match multiple files. 1. all the data in the files are numbers e.g. 1234567 1584752 2563156 2. each sorted file have their own ouput. e.g. test.csv -> test_sorted.csv 3. Then, I need to match all... (4 Replies)
Discussion started by: nazri76
4 Replies
Login or Register to Ask a Question