dimanche 9 juin 2019

How to replace a word ending in something with another word in a file

I have a file called sentry.tmp with structure

8272275 docliente_up/ant_aux_secciones_mes2 556321
8272276 docliente_up/ant_aux_ufcp_mes1  556322
8272278 docliente_up/ant_aux_ufcp_mes2  556324
8272460 docliente_up/jmn_sms_sevilla    556471
8837577 docliente_bu/do_acumulacion/fecha=20190502  550972
8837578 docliente_tres/do_acumulacion/fecha=20190524    550972
8837579 docliente/do_acumulacion/fecha=20190503 550972

I like to add the word databases before words ending in _up/ that are in the beginning of the second columns, and the others add the word databases_users.

Something like this:

8272275 databases/docliente_up/ant_aux_secciones_mes2   556321
8272276 databases/docliente_up/ant_aux_ufcp_mes1    556322
8272278 databases/docliente_up/ant_aux_ufcp_mes2    556324
8272460 databases/docliente_up/jmn_sms_sevilla  556471
8837577 databases_users/docliente_bu/do_acumulacion/fecha=20190502  550972
8837578 databases_users/docliente_tres/do_acumulacion/fecha=20190524    550972
8837579 databases_users/docliente/do_acumulacion/fecha=20190503 550972

I have a file that have the name of the first word of the second column.

$cat init.txt
 docliente_up
 docliente_bu
 docliente_tres
 docliente

and I use this code but I can't find the way to do it.

for i in `awk '{print $2}' $wkdir/init.txt`
 do 
if  [[ "$i" == *_up ]]
 then 
  awk -v basedatos=$i '{gsub(/$basedatos/,"databases/$basedatos");}1' sentry.tmp >sentry.new
 else 
  awk -v basedatos=$i '{gsub(/$basedatos/,"databases_users/$basedatos");}1' sentry.tmp >>sentry.new
 fi
done

Aucun commentaire:

Enregistrer un commentaire