LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2011, 01:02 PM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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!

Last edited by cristalp; 10-27-2011 at 01:28 PM.
 
Old 10-27-2011, 02:20 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, awk can do the trick:
Code:
awk '{getline line < "file2"; gsub(/ /,",",line); sub(/FRAC/,line); print}' file1
 
1 members found this post helpful.
Old 10-28-2011, 09:18 AM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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!

Last edited by cristalp; 10-28-2011 at 09:20 AM.
 
Old 10-28-2011, 09:24 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 10-28-2011, 09:41 AM   #5
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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!!!
 
Old 10-28-2011, 09:47 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by cristalp View Post
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 View Post
Thanks!!!
You're welcome!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 02:24 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
bash: how to replace strings of a file with some " chars ? frenchn00b Linux - General 1 03-01-2008 09:10 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration