Update a file using other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Update a file using other file
# 1  
Old 12-14-2015
Update a file using other file

Gents,

Please help.
Matching fields 2 and 3 in file1 and file2, I will like to update the file 2 using the information (column 48 to 71) from file 1

examples

file1
Code:
S  40149.00  47897.00  11                      453310.8 2443363.8 131.9348000413                               
S  40573.00  47897.00  11                      453312.3 2448664.5 131.4348000422                               
S  40147.00  47897.00  11                      453311.9 2443338.5 132.2348000450                               
S  40575.00  47897.00  11                      453311.9 2448687.9 131.2348000458                               
S  40145.00  47897.00  11                      453312.7 2443313.0 131.9348000526

File2
Code:
S  40149.00  47897.00  1V1     0.0   0     0.0 453312.5 2443362.5   0.0348000414                               
S  40147.00  47897.00  1V1     0.0   0     0.0 453312.5 2443337.5   0.0348000451                               
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.5 2443312.5   0.0348000527                               
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.5 2448662.5   0.0348000423                               
S  40575.00  47897.00  1V1     0.0   0     0.0 453312.5 2448687.5   0.0348000459

output
Code:
S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414                               
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451                               
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527                               
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423                               
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459

Thanks for your help.
# 2  
Old 12-14-2015
Code:
awk 'FNR==NR {k[$2,$3]=substr($0, 48, 24);next} ($2,$3) in k{sub(substr($0, 48, 24), k[$2,$3])}1' file1 file2

This User Gave Thanks to Aia For This Post:
# 3  
Old 12-14-2015
Code:
$ 
$ cat file1
S  40149.00  47897.00  11                      453310.8 2443363.8 131.9348000413
S  40573.00  47897.00  11                      453312.3 2448664.5 131.4348000422
S  40147.00  47897.00  11                      453311.9 2443338.5 132.2348000450
S  40575.00  47897.00  11                      453311.9 2448687.9 131.2348000458
S  40145.00  47897.00  11                      453312.7 2443313.0 131.9348000526
$ 
$ cat file2
S  40149.00  47897.00  1V1     0.0   0     0.0 453312.5 2443362.5   0.0348000414
S  40147.00  47897.00  1V1     0.0   0     0.0 453312.5 2443337.5   0.0348000451
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.5 2443312.5   0.0348000527
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.5 2448662.5   0.0348000423
S  40575.00  47897.00  1V1     0.0   0     0.0 453312.5 2448687.5   0.0348000459
$ 
$ perl -F'/\s+/' -lane '$ARGV eq "file1"
                        ? $x{join("|",@F[1,2])} = substr($_,47)
                        : do {$k = join("|",@F[1,2]); substr($_,47) = $x{$k} if defined $x{$k}; print}
                       ' file1 file2
S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000413
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000450
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000526
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000422
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000458
$ 
$

These 2 Users Gave Thanks to durden_tyler For This Post:
# 4  
Old 12-15-2015
Hi, durden_tyler

Nice work, however your output is a bit off of what it is wanted if I am interpreting correctly.
Quote:
Originally Posted by durden_tyler
Code:
$ perl -F'/\s+/' -lane '$ARGV eq "file1"
                        ? $x{join("|",@F[1,2])} = substr($_,47)
                        : do {$k = join("|",@F[1,2]); substr($_,47) = $x{$k} if defined $x{$k}; print}
                       ' file1 file2

Code:
  Your output: S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000413
Wanted output: S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414 
  Your output: S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000450
Wanted output: S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451
  Your output: S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000526
Wanted output: S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527
  Your output: S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000422
Wanted output: S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423
  Your output: S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000458
Wanted output: S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459

I was thinking more in the following line:
Code:
perl -anle '$ARGV eq "file1" ? $p{"@F[1,2]"} = substr($_, 47, 24) : do{ if(defined $p{"@F[1,2]"}){substr($_, 47, 24, $p{"@F[1,2]"})}print}' file1 file2

Output:
Code:
S  40149.00  47897.00  1V1     0.0   0     0.0 453310.8 2443363.8 131.9348000414
S  40147.00  47897.00  1V1     0.0   0     0.0 453311.9 2443338.5 132.2348000451
S  40145.00  47897.00  1V1     0.0   0     0.0 453312.7 2443313.0 131.9348000527
S  40573.00  47897.00  1V1     0.0   0     0.0 453312.3 2448664.5 131.4348000423
S  40575.00  47897.00  1V1     0.0   0     0.0 453311.9 2448687.9 131.2348000459

These 2 Users Gave Thanks to Aia For This Post:
# 5  
Old 12-15-2015
Thanks for pointing it out, Aia.
jiam912's specification was very precise, but I missed it.
Pretty good use of replacement within substr() function in your code.
Cheers!
This User Gave Thanks to durden_tyler For This Post:
# 6  
Old 12-16-2015
Aia,

Thanks a lot
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. Shell Programming and Scripting

Move file from one directory and update the list file once moved.

Dears, I have a listfile contains list of files path. i need to read the line of the listfile mv the file to other directory and update the listfile by deleting the lines of the listfile. #!/bin/bash target=/fstest/INVESTIG/Sadiq/TEST_ARCH while read -r line || ]; do mv $line... (19 Replies)
Discussion started by: sadique.manzar
19 Replies

3. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Perl to update field in file based of match to another file

In the perl below I am trying to set/update the value of $14 (last field) in file2, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Update a file with other file data

Gents, Can you help please. Using information from file1, I will like to update file2. file1 X 2922 1581L 30535.00 51881.001 1 3241 30540.00 51666.00 52312.001 X 2922 8931L 30961.00 52625.001 1 4321 30540.00 52194.00 53056.001 X 2922 1711L 30529.00 ... (2 Replies)
Discussion started by: jiam912
2 Replies

6. Shell Programming and Scripting

File Update

looking for an Update file command in Unix File A|B|C|D|NAMND|B A|B|C|DD|N|B A|B|C|DD|AAM|L new file should be A|B|C|D|N|B A|B|C|DD|N|B A|B|C|DD|A|L awk -F"|" 'length($5)>1' ----all these records to be updated with 5th filed first charecter (1 Reply)
Discussion started by: userraone
1 Replies

7. Shell Programming and Scripting

rsync - update file on backup when file renamed on source

hi all, Please help me with rsync. I configured rsync to preserve timestamps using the -a option. When i renamed fileA to fileB on source machine I have to copies at the backup server. The aim is to keep the most recent file. fileA & fileB has same contents. When i renamed fileB to... (2 Replies)
Discussion started by: coolatt
2 Replies

8. Shell Programming and Scripting

how to update a file

-Hi, I have several hundred files, which contain the following pattern: /bb/bin/msga/mm 80& I need to change the above pattern to be /bb/bin/mm 80& I there the command I can use to do that. Thanks a lot (3 Replies)
Discussion started by: aoussenko
3 Replies

9. Shell Programming and Scripting

update a file with values from other file in shell bash

Hello I need to write a shell script to update properties between files. I have 2 files as follow: file1 urlWebserviceCheckAddress^=McoConfigGlobal.commonGLOBALUrlWebservice file 2 urlWebserviceCheckAddress=http://localhost:8080/tags Both files containt the same properties... (1 Reply)
Discussion started by: teodora
1 Replies

10. Shell Programming and Scripting

file update

how to update file in unix ? (8 Replies)
Discussion started by: piyush_movadiya
8 Replies
Login or Register to Ask a Question