2

Alright, I cannot figure out how to do this.

I have a fileA which looks like this :

([7]RIMS_ID)                                            : "CNR"       
(refGain_A[7])                                          : 1           
(RIMSclockBias_A[7])                                    : -398015316.7
(RIMSclockDrift_A[7])                                   : -6442.29    
(RIMSclockSigma_A[7])                                   : .01         
(RIMSclockSigY_A[7])                                    : 0        

([8]RIMS_ID)                                            : "ABS"       
(refGain_A[8])                                          : 1           
(RIMSclockBias_A[8])                                    : -374515458
(RIMSclockDrift_A[8])                                   : -6442.29    
(RIMSclockSigma_A[8])                                   : .01         
(RIMSclockSigY_A[8])                                    : 0     

and so on where the [index] goes from 0 to 71 and each station has a different ID.

I want to replace the value corresponding to RIMSclockBias_A string of each station with a value I have on another fileB like this:

CNR -44163754.49
ABS 3417370.112
...

So to have:

([7]RIMS_ID)                                            : "CNR"       
(refGain_A[7])                                          : 1           
(RIMSclockBias_A[7])                                    : -44163754.49
(RIMSclockDrift_A[7])                                   : -6442.29    
(RIMSclockSigma_A[7])                                   : .01         
(RIMSclockSigY_A[7])                                    : 0        

([8]RIMS_ID)                                            : "ABS"       
(refGain_A[8])                                          : 1           
(RIMSclockBias_A[8])                                    : 3417370.112
(RIMSclockDrift_A[8])                                   : -6442.29    
(RIMSclockSigma_A[8])                                   : .01         
(RIMSclockSigY_A[8])                                    : 0 

I can isolate the correct field with a combination of grep in a for loop and awk, but I don't know how I can replace the value in the file itself. sed needs to know the exact value I want to replace as input so it is not feasible.

Any idea?

1

This is closely related to another question only in this case the substitution has to be made on the first line that matches clockBias after the pattern. If there was no special character in your fileB you could run with gnu sed :

sed -E 's|(.*)[[:blank:]](.*)|/\1/,/clockBias/{/clockBias/{s/(:[[:blank:]]{1,})(.*)/\\1\2/}}|' fileB | sed -Ef - fileA

That simply turns the lines in your fileB into sed commands e.g.:

/CNR/,/clockBias/{/clockBias/{s/(:[[:blank:]]{1,})(.*)/\1-44163754.49/}}

and then passes them to the second sed to process fileA.


With unknown input you'd have to escape any special characters in the LHS/RHS as I've explained in my answer to the other question (this time using BRE syntax):

sed 's|\(.*\)[[:blank:]]\{1,\}\(.*\)|\1\
\2|
h
s|.*\n||
s|[\&/]|\\&|g
x
s|\n.*||
s|[[\.*^$/]|\\&|g
G
s|\(.*\)\n\(.*\)|/\1/,/clockBias/{/clockBias/{s/\\(:[[:blank:]]\\{1,\\}\\)\\(.*\\)/\\1\2/}}|' fileB | sed -f - fileA
  • What about if the pattern is not always on the 2 line after the match? Would be any smarter way to tell him to do the replace only in correspondence of ClockBias string? – Dad85 May 23 '16 at 16:16
  • Thanks don_crissti. By the way it is not working properly. Running your code it makes "strange" substitutions, e.g.: ABS (RIMSclockBias_A[0]) : 4536230.86 -> (RIMSclockBias_A[0]) : -490697.2517 or for ALB (RIMSclockBias_A[2]) : 11074942.05 -> (RIMSclockBias_A[2]) : -490697.2517 and so onwhile my fileB has: ABS -44163754.49 ALB -37625043.3 I don't see where is the problem with your code. – Dad85 May 24 '16 at 7:46
  • @Dad85 - using your input sample my code above works absolutely fine (try for yourself... I've even created a file 500 lines long with the same structure and with clockBias in random locations in every paragraph and it still works OK). I can only work with the input sample that you provide so I cannot know what's wrong on your side... (there could be a lot of things from hidden funky chars in your file to windows line endings or whatnot). – don_crissti May 24 '16 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.