LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: replace strings in on file by the strings in another one (https://www.linuxquestions.org/questions/programming-9/bash-replace-strings-in-on-file-by-the-strings-in-another-one-910508/)

cristalp 10-27-2011 01:02 PM

BASH: replace strings in on file by the strings in another one
 
Dear Experts,

I have two files. One with lines:
Code:

{{2.9,17},{136.178,117.917,333.375},{FRAC}},
{{3.3,17},{136.853,114.895,331.039},{FRAC}},
{{2.4,13},{136.711,117.909,331.126},{FRAC}},
{{2.5,13},{135.686,116.092,330.503},{FRAC}},
{{3.3,14},{309.961,102.309,96.707},{FRAC}},
...

another with lines:
Code:

-39.1562 49.1725 55.4316
-24.6125 44.5893 57.9897
-17.1902 43.8194 55.6517
-18.2794 36.0131 57.1200
-53.9493 -49.0509 -57.4066
...

I want to replace the symbol "FRAC" in each line of the first file by the 3 numbers in the corresponding line of the second file. The line numbers should match among two files. And when the 3 numbers are put in the first file, they should keep in the same format separated by "," but not space.

So, the output file should look like:
Code:


{{2.9,17},{136.178,117.917,333.375},{-39.1562,49.1725,55.4316}},
{{3.3,17},{136.853,114.895,331.039},{-24.6125,44.5893,57.9897}},
{{2.4,13},{136.711,117.909,331.126},{-17.1902,43.8194,55.6517}},
{{2.5,13},{135.686,116.092,330.503},{-18.2794,36.0131,57.1200}},
{{3.3,14},{309.961,102.309,96.707},{-53.9493,-49.0509,-57.4066}},
...

Could this be done simply by AWK or some way in bash scripting?

Any answer would be appreciated. Thanks a lot!

colucix 10-27-2011 02:20 PM

Yes, awk can do the trick:
Code:

awk '{getline line < "file2"; gsub(/ /,",",line); sub(/FRAC/,line); print}' file1

cristalp 10-28-2011 09:18 AM

Quote:

Originally Posted by colucix (Post 4510052)
Yes, awk can do the trick:
Code:

awk '{getline line < "file2"; gsub(/ /,",",line); sub(/FRAC/,line); print}' file1

Thanks. I tried it. it gave me the output:
Code:

{{5.1,26},{317.814,97.393,345.404},{-21.99705,2.48297,19.17858,}},
{{3.5,26},{63.147,117.231,171.417},{18.77043,7.61240,0.14598,}},
{{3.3,25},{228.533,129.016,345.199},{0.88855,11.90227,38.40225,}},
{{3.5,24},{149.448,92.877,170.309},{17.64314,-9.89942,-19.29017,}},
{{2.9,24},{219.508,140.034,337.012},{0.85668,-10.05760,38.27079,}},
{{4.0,24},{147.708,81.198,164.822},{7.36717,-15.39773,-19.52328,}},

Note the redundant "," at the end of the numbers

What I really need is:
Code:

{{2.9,17},{136.178,117.917,333.375},{-39.1562,49.1725,55.4316}},
{{3.3,17},{136.853,114.895,331.039},{-24.6125,44.5893,57.9897}},
{{2.4,13},{136.711,117.909,331.126},{-17.1902,43.8194,55.6517}},
{{2.5,13},{135.686,116.092,330.503},{-18.2794,36.0131,57.1200}},
{{3.3,14},{309.961,102.309,96.707},{-53.9493,-49.0509,-57.4066}},

which has no comma at the end of the third number in the third string. How could I solve it? Thanks!

colucix 10-28-2011 09:24 AM

This is because the second file has an extra space at the end of the lines, so that they are substituted with a comma by the gsub function. You can try to add another gsub to remove them before doing the substitution, e.g. (see the part highlighted in red)
Code:

awk '{getline line < "file2"; gsub(/ +$/,"",line); gsub(/ /,",",line); sub(/FRAC/,line); print}' file1

cristalp 10-28-2011 09:41 AM

Quote:

Originally Posted by colucix (Post 4510615)
This is because the second file has an extra space at the end of the lines, so that they are substituted with a comma by the gsub function. You can try to add another gsub to remove them before doing the substitution, e.g. (see the part highlighted in red)
Code:

awk '{getline line < "file2"; gsub(/ +$/,"",line); gsub(/ /,",",line); sub(/FRAC/,line); print}' file1

Thanks a lot!

Just one more question, could you explain what the meaning of the regular expression in
Code:

gsub(/ +$/,...)
Why it can locate to the last space? Because there is no "^" in the regular expression? And why you need a "+" before "$"?

Thanks!!!

colucix 10-28-2011 09:47 AM

Quote:

Originally Posted by cristalp (Post 4510628)
Why it can locate to the last space? Because there is no "^" in the regular expression? And why you need a "+" before "$"?

The regular expression
Code:

/ +$/
matches one or more (+) spaces ( ) at the end of each line ($). You don't need the (^) sign if you don't want to match anything at the beginning of the string. Regarding the plus sign I added it to take care of multiple spaces at the end of the lines (if any) but you don't really need it if you're sure the file contains at most one space character at the end of every line.
Quote:

Originally Posted by cristalp (Post 4510628)
Thanks!!!

You're welcome!


All times are GMT -5. The time now is 11:57 PM.