Register
It is currently Thu Nov 27, 2014 6:15 pm

random substitution with awk


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue May 03, 2011 10:24 am   

Joined: Tue May 03, 2011 10:20 am
Posts: 3
Dear all,
I am a new user of the forum and of awk.
I have the same problem:
I have a file in the following format:

Si -7.87760000 6.16710000 16.80090000
Si -2.20190000 8.96480000 18.80290000
Si -3.91340000 6.18110000 16.79500000
Si -5.89320000 3.44010000 18.81540000
Si -5.89980000 7.45760000 17.18100000
H -7.84830000 0.67980000 16.84620000
H -7.86760000 4.81220000 18.78920000
Si 2.20250000 8.96490000 18.80280000
Si -0.00010000 6.26880000 16.82620000
Si -1.94640000 3.46010000 18.83720000
H -1.99140000 7.62480000 16.81710000
Si -3.91150000 0.68930000 16.84840000
Si -3.92500000 4.81880000 18.78390000
Si -5.88640000 -2.06440000 18.85090000
H -5.88640000 2.06580000 16.83280000
Si -7.86620000 -4.81050000 16.89460000
Si -7.84800000 -0.67730000 18.83550000
Si 3.91310000 6.18150000 16.79530000
Si 1.94640000 3.46000000 18.83770000
Si 1.99110000 7.62510000 16.81700000

I would like to substitute in a random way the symbol "Si" with the symbol "Ge" in the first column. This substitution should not happen when the line contains the symbol H. I have tried to start with this script, but it doesn't work:

#!/usr/bin/awk -f

#

# Usage:

# ./impurity_gen.awk -v NIMP=12 -v SYMB=Ge

#



BEGIN{

natom=16;
### this is the total number of lines containing the symbol "Si"
nimp=NIMP;
### this the number of lines I would like to substitute
symb=SYMB;
### this is the symbol with whom I'd like to substitute Si
srand()

for (j = 1; j <= nimp; ++j) {

# loop to find a not-yet-seen selection

do {

select = 1 + int(rand() * natom)

} while (select in pick)

pick[j] = select

}

}



NF != 4 { next }



which_Si = 0

symb_tmp = $1



if ( /Si/ ) {

which_Si += 1

do {

symb_tmp=symb

} while ( which_Si in pick )



x=$2; y=$3; z=$4;

printf "%5s %15.9f %15.9f %15.9f \n", symb, x, y, z

}

Please can you give any suggestions or solutions to this problem???
Thank you very much in advance


Top
 Profile  
 PostPosted: Tue May 03, 2011 3:49 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
I'd probably use perl:
Code:
perl -pe 's/Si/Ge/ if int rand 2'


Top
 Profile  
 PostPosted: Tue May 03, 2011 5:20 pm   

Joined: Tue May 03, 2011 10:20 am
Posts: 3
Many thanks for your reply!
Do you think that with perl would it be possible to obtain a script that every time you run it give different configurations for the same number of lines to change??
In other words: for example my input file contains 20 lines with Si and I want to make 6 change with Ge, but in six different modes.
Do you think is it possible?? How?
Thank you very much!


Top
 Profile  
 PostPosted: Wed May 04, 2011 9:57 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
fattobenesi wrote:
Many thanks for your reply!
Do you think that with perl would it be possible to obtain a script that every time you run it give different configurations for the same number of lines to change??
In other words: for example my input file contains 20 lines with Si and I want to make 6 change with Ge, but in six different modes.
Do you think is it possible?? How?
Thank you very much!

Of course it is possible, but not so easy :) How would you do it by hand? In other words, first, try to formulate the algorithm, then implement it in whatever language you know.


Top
 Profile  
 PostPosted: Thu May 05, 2011 8:35 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
OK, I had an idea later how to make it without perl, here is the solution:
Code:
changes=6
grep -n ^Si 1368.txt | cut -f1 -d: | sort -R | head -n $changes | sed 's/$/s%Si%Ge%/'  | sed -f- 1368.txt

Beware: this code does 6 (or whatever you set $changes to) random changes. Sometimes, though, it can return the same output as it did before.


Top
 Profile  
 PostPosted: Sun May 29, 2011 7:19 am   

Joined: Tue May 03, 2011 10:20 am
Posts: 3
Many thanks!!!!
It works very well!!!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP