pistal
pistal

Reputation: 2456

Regex replacement using sed and awk

I'm trying to replace first, second and third %f with a b and c respectively using sed and awk.

(51.7296373326*%f)+(41.7319764456*%f)+(-193.993966414*%f)

This is what i've tried

sed "s|(51.7296373326*%f)|(51.7296373326*a)|g" dump1.txt  

The values are not constant..

Upvotes: 1

Views: 45

Answers (1)

Kent
Kent

Reputation: 195039

I guess you meant sed or awk.

awk 'BEGIN{a[++i]="a";a[++i]="b";a[++i]="c"}
     {for(x=1;x<=i;x++)sub(/%f/,a[x])}7' file

if the replacement list is big, like a-z, you can put them in a file, first load it into array a instead of loading them in BEGIN block.

Note, the codes could be optimized by checking the return value of sub, if 0, break.

Upvotes: 1

Related Questions