Tuesday, March 22, 2016

substitute number with sed

I want to automatically automatically substitute the "number of levels" of the pedigree in a blupf90 parameter file. For that, I keep a basic.par:

DATAFILE
data
NUMBER_OF_TRAITS
           1
NUMBER_OF_EFFECTS
           2
OBSERVATION(S)
3
WEIGHT(S)

EFFECTS: POSITIONS_IN_DATAFILE NUMBER_OF_LEVELS TYPE_OF_EFFECT[EFFECT NESTED]
1 1 cross
2 30000 cross
RANDOM_RESIDUAL VALUES
0.7
 RANDOM_GROUP
           2
 RANDOM_TYPE
add_animal
 FILE
ped                                                
(CO)VARIANCES
0.3

then I read the actual number of levels in ped:


# get number in pedigree

n=$(wc -l ped | awk '{print $1}')

in my example, 2800
and I use sed to modify the 30000 to 'n':


# change 2nd number in line 13 to n 

sed  '13 s/[0-9]* /'$n' /2' < basic.par > new.par

I am not good with regular expressions, but I believe this means "in line 13, any number ([0-9] repeated from 0 to infinite times -that's what * means' and only substitute the 2nd occurrence".  The spaces in the regular expression are by trial and error :-(  
The little that I know about sed comes from the Grymoire .

No comments:

Post a Comment