通过加法和乘法进行文本操作 [英] text manipulation by addition and multiplication
问题描述
我有一个保存在名称"test_file"中的文本文件,其中包含6行和7列,如下所示
I have a text file saved in the name 'test_file' that contain 6 rows and 7 columns as given below
0.00 5.8 2.0 5.0 6.0 8.0 0.0
10.00 5.8 2.0 1.0 1.0 1.2 9.6
10.00 9.3 2.2 2.0 1.4 2.5 9.6
30.00 9.3 2.2 1.2 1.5 1.9 1.4
30.00 9.3 2.2 3.2 2.4 1.2 4.1
60.00 9.8 3.5 1.4 2.7 3.2 4.5
我想在第二列和第三列中进行一些文本操作.
I want to do some text manipulation in second and third column.
在第三列中,前两行值应相同(2.0和2.0),而后三行值仅是第二行值的0.2增量(2.0 + 0.2 = 2.2、2.0 + 0.2 = 2.2、2.0 + 0.2 = 2.2).但是,我不想更改我想保留的最后一行.
In the third column first two rows values should be same (2.0 and 2.0) and next three rows values are just the 0.2 increment of second row value(2.0+0.2=2.2,2.0+0.2=2.2,2.0+0.2=2.2).However, i don't want to change last row i want to keep it as it is.
在第二列之后,前两行的值应该是第三列的前两行与2.9的乘积. 同样,第二列的后三行只是第三列的后三行与4.227的乘积
After that in the second column, first two rows values should be just the multiplication of, first two rows of third column with 2.9. similarly next three rows of second column are just the multiplication of next three rows of third column with 4.227
我根本不想更改的其他列值.
other columns values i don't want to change at all.
现在我想依次更改第三列的前两行值,即2.1、2.2 .... 2.5,然后再进行相同的增量和乘法.
Now i want to change the first two rows value of third column sequentially, 2.1,2.2....2.5 followed by same increment and multiplication.
例如,当我将第三列的前两行值从原始2.0更改为2.1时,预期输出应为
For example when i change first two rows values of third column from original 2.0 to 2.1 then the expected output should be
0.00 6.09 2.1 5.0 6.0 8.0 0.0
10.00 6.09 2.1 1.0 1.0 1.2 9.6
10.00 9.722 2.3 2.0 1.4 2.5 9.6
30.00 9.722 2.3 1.2 1.5 1.9 1.4
30.00 9.722 2.3 3.2 2.4 1.2 4.1
60.00 9.8 3.5 1.4 2.7 3.2 4.5
我想用其他名称保存输出文件,例如file2.1.txt .... file2.5.txt
and i want to save the output file in different name such as file2.1.txt....file2.5.txt
推荐答案
如果不能与其他答案一起使用,则为另一个版本:
Here's another version if you can't work with the other answer:
awk -vval=2.1 '{ # set "val" to the new value for column 3 on first two lines
if(NR==1 || NR==2) { # if it's the first or second line
$3=val; # set column 3 to val
$2=$3*2.9 # set column 2 to column 3 multiplied with 2.9
} else if(NR>=3 && NR<=5) { # else if it's line 3-5
$3=val+0.2; # set column 3 to val+0.2
$2=$3*4.227 # set column 2 to column 3 multiplied with 4.227
} else $3=$3; # just for formatting
print # print the result
}' test_file
在运行注释之前将其删除(#
).
Remove the comments (#
) before you run it.
输出:
0.00 6.09 2.1 5.0 6.0 8.0 0.0
10.00 6.09 2.1 1.0 1.0 1.2 9.6
10.00 9.7221 2.3 2.0 1.4 2.5 9.6
30.00 9.7221 2.3 1.2 1.5 1.9 1.4
30.00 9.7221 2.3 3.2 2.4 1.2 4.1
60.00 9.8 3.5 1.4 2.7 3.2 4.5
要遍历某个范围并将其保存在其他文件中,您可以执行以下操作.我还提供了其他参数,以便您在运行脚本时可以设置它们:
To loop over a range and save it in different files you can do like below. I also made the other parameters available so you can set them when running the script:
#!/bin/bash
for val in $(seq 2.1 0.1 2.5)
do
awk -vval=$val -vfmul=2.9 -vadd=0.2 -vsmul=4.227 '{
if(NR==1 || NR==2) {
$3=val;
$2=$3*fmul
} else if(NR>=3 && NR<=5) {
$3=val+add;
$2=$3*smul
} else $3=$3;
print
}' test_file > output$val
done
这篇关于通过加法和乘法进行文本操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!