Мне нужно умножить числовые значения на константу, скажем, 0,9 для чисел, следующих за шаблоном «-period». Только числа после шаблона «-period» необходимо умножать, но не для любых других чисел, поскольку в файле будет много числовых значений.
Please provide correct awk
command options to modify the values in the file.
awk -F, '{$3=$3*0.9;print}' OFS=, inputfile.txt
is not useful as these numbers are not in a single column.
Contents of the file :
clock -period 5.00 -name clk1
clock -name txclk2 -period 10.00
clock -period 7.00 -name clk3
input_delay -clock clk1 4.0
After modification it has to be:
clock -period 4.50 -name clk1
clock -name txclk2 -period 9.00
clock -period 6.30 -name clk3
input_delay -clock clk1 4.0
I don't think there's a simpler way to do this in awk than to iterate over the fields:
$ awk '{for(i=1;i<NF;i++){if($i == "-period") $(i+1) *= 0.9}} 1' inputfile.txt
clock -period 4.5 -name clk1
clock -name txclk2 -period 9
clock -period 6.3 -name clk3
input_delay -clock clk1 4.0
If you want the specific floating point format, then
$ awk '{for(i=1;i<NF;i++){if($i == "-period") $(i+1) = sprintf("%.2f",0.9*$(i+1))}} 1' inputfile.txt
clock -period 4.50 -name clk1
clock -name txclk2 -period 9.00
clock -period 6.30 -name clk3
input_delay -clock clk1 4.0
Note: I omitted -F,
and OFS=","
since your files as shown are whitespace delimited.