Question

I have a text file organized like this:

beep(E1, 128);

beep(D2, 137);

beep(F0, 36);

beep(E2, 36);

beep(E2, 41);

. . .

and I need to multiply the second integer in each line by a given constant, with the first integer being unchanged and the output being an integer (could be next up, next down or nearest from multiplied answer; it doesn't matter much). There won't be a time when the first integer goes above 10 or the second one goes below 10, if that helps. Here's an example output given a multiplier of .5:

beep(E1, 64);

beep(D2, 69);

beep(F0, 18);

beep(E2, 18);

beep(E2, 21);

. . .

I realize this is similar to this question: Awk to multiply all numbers in a text file by a constant subject to another constraint, but I didn't feel the answers there suited my needs. Any and all help towards a solution is greatly appreciated.

Was it helpful?

Solution

After reading your question and following your link I decided to learn awk. I believe this will solve your problem. 'p' is used as your constant.

Linux:

awk '{if(length($0)>0){p=0.5;print $1,int(substr($2,0,length($2)-1)*p)");"}}' filename

Windows:

awk "{if(length($0)>0){p=0.5;print $1,int(substr($2,0,length($2)-1)*p)\");\"}}" filename

Both versions have been tested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top