【发布时间】:2020-09-25 15:37:00
【问题描述】:
我知道类似的问题已经在 SO 上回答了很多次。
(一个例子是here: https://stackoverflow.com/questions/7846476/replace-column-in-one-file-with-column-from-another-using-awk)
但是,这对我来说是独一无二的,因为我需要处理特定的模式。
我想要更新的文件 1 的标题是
3 6 0 6.0361821 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
0.994429353 0.000000000 0.000000000
0.000000000 0.994429353 0.000000000
0.000000000 0.000000000 2.469627493
1 'A ' 63548.626894188397
2 'B ' 169717.29799472401
3 'C ' 25598.367262405900
1 2 0.7458220147 0.7458220147 1.8031927376 << need to be updated from here
2 2 0.2486073382 0.2486073382 0.6664347554
3 1 0.2486073382 0.2486073382 2.2628589536
4 1 0.7458220147 0.7458220147 0.2067685394
5 3 0.7458220147 0.7458220147 1.0275486366
6 3 0.2486073382 0.2486073382 1.4420788564 << upto here
T
21.3496599 0.0000000 0.0000000
0.0000000 21.3496599 0.0000000
0.0000000 0.0000000 24.1101752
1
-7.6119990 -0.0000000 0.0000000
0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
2
-7.6119990 0.0000000 0.0000000
-0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
3
3.4711749 0.0000000 0.0000000
我需要将file1 的$2、$3 和$4 从$ESPi"th 行更新为"$ESPf"th 和$1、$2 和$3(提到@9876543333)以下)。 file1 中的空格在更新时不应更改。这里$ESPi"th 和"$ESPf"th 分别代表第8 行和第13 行,并且大小写不同。
file2 是
0.750000000 0.750000000 0.730147661 << with these data
0.250000000 0.250000000 0.269852339
0.250000000 0.250000000 0.916275414
0.750000000 0.750000000 0.083724586
0.750000000 0.750000000 0.416074343
0.250000000 0.250000000 0.583925657 < upto these data
我已经尽力做好我的工作了。
#!/bin/bash
for j in `seq "$ESPi" 1 "$ESPf"` # ESPi and ESPf are 8 and 13, respectively here and change case by case.
do
ESP1=$(cat file1 | head -n "$j" | tail -n 1 | awk '{print $3}')
ESP2=$(cat file1 | head -n "$j" | tail -n 1 | awk '{print $4}')
ESP3=$(cat file1 | head -n "$j" | tail -n 1 | awk '{print $5}')
for k in `seq 1 1 "$NELEMENTS"` # $NELEMENTS is six here.
do
qeIN1=$(cat file2 | head -n "$k" | tail -n 1 | awk '{print $1}')
qeIN2=$(cat file2 | head -n "$k" | tail -n 1 | awk '{print $2}')
qeIN3=$(cat file2 | head -n "$k" | tail -n 1 | awk '{print $3}')
sed 's/'$ESP1'/'$qeIN1'/g' file1
sed 's/'$ESP2'/'$qeIN2'/g' file1
sed 's/'$ESP3'/'$qeIN3'/g' file1
done
done
这给了我
3 6 0 6.0361821 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
0.994429353 0.000000000 0.000000000
0.000000000 0.994429353 0.000000000
0.000000000 0.000000000 2.469627493
1 'A ' 63548.626894188397
2 'B ' 169717.29799472401
3 'C ' 25598.367262405900
1 2 0.7458220147 0.7458220147 1.8031927376
2 2 0.750000000 0.750000000 0.6664347554
3 1 0.750000000 0.750000000 2.2628589536
4 1 0.7458220147 0.7458220147 0.2067685394
5 3 0.7458220147 0.7458220147 1.0275486366
6 3 0.750000000 0.750000000 1.4420788564
T
21.3496599 0.0000000 0.0000000
0.0000000 21.3496599 0.0000000
0.0000000 0.0000000 24.1101752
1
-7.6119990 -0.0000000 0.0000000
0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
2
-7.6119990 0.0000000 0.0000000
-0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
3
3.4711749 0.0000000 0.0000000
预期的输出是
3 6 0 6.0361821 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
0.994429353 0.000000000 0.000000000
0.000000000 0.994429353 0.000000000
0.000000000 0.000000000 2.469627493
1 'A ' 63548.626894188397
2 'B ' 169717.29799472401
3 'C ' 25598.367262405900
1 2 0.750000000 0.750000000 0.730147661
2 2 0.250000000 0.250000000 0.269852339
3 1 0.250000000 0.250000000 0.916275414
4 1 0.750000000 0.750000000 0.083724586
5 3 0.750000000 0.750000000 0.416074343
6 3 0.250000000 0.250000000 0.583925657
T
21.3496599 0.0000000 0.0000000
0.0000000 21.3496599 0.0000000
0.0000000 0.0000000 24.1101752
1
-7.6119990 -0.0000000 0.0000000
0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
2
-7.6119990 0.0000000 0.0000000
-0.0000000 -7.6119990 0.0000000
0.0000000 0.0000000 -7.0331945
3
3.4711749 0.0000000 0.0000000
我正在寻找 shell (bash) 脚本。
【问题讨论】:
-
将您的脚本复制/粘贴到shellcheck.net,修复它告诉您的问题,如果您仍然需要帮助,请重新发布。