954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Changing Text Files

Hi everyone,

I have a text file which looks like this:

( 3,3,0 )       0.9984104       0.000503
( 3,3,1 )       1.000613        0.000484
( 3,3,2 )       1.001192        0.000571
( 3,3,3 )       1.002418        0.00063
( 3,3,4 )       1.003345        0.000583
( 3,3,5 )       1.000215        0.000512
( 3,3,6 )       1.000911        0.000537
( 3,3,7 )       0.9983607       0.000423


I want to change it to the following form.

0       0.9984104       0.000503   
1       1.000613        0.000484   
2       1.001192        0.000571   
3       1.002418        0.00063        
4       1.003345        0.000583        
5       1.000215        0.000512        
6       1.000911        0.000537        
7       0.9983607       0.000423


There is one way that came to my mind and it was replacing each (3,3,i) with i using sed command but I believe that it is not the most intelligent way of doing it. Can anyone explain to me how to solve this problem or give me some links that can help me?

SakuraPink
Light Poster
31 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

sed would be perfect for this. You could also use cut -c if all the tuple-numbers are one digit, or cut -f twice: Once with -d ',' and once with -d ')'

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 
awk '{split($0, a, ","); sub(/\)/,"",a[3]); print a[3]}' source.file > result.file


Or

sed 's!( [0-9],[0-9],\([0-9]\) )!\1!' source.file > result.file
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You