bash 用于更改文本文件的脚本
bash script for changing text file
我有一个名为 params.dat 的数据文件,我想在每次 运行 我的代码时更改文件中的值。
这是我到目前为止得到的
i=7.0
k=0
i=0
while [ $i -lt 10]
do
sed "3s/.*/$j 6.9 $j/" "28s/.*/image$i.bmp/" params.dat
((i++))
((k++))
((j=j-0.1))
done
目标是更改日期文件的第 3 行和第 28 行
7.0 7.0 7.0
到
6.9 7.0 6.9
基本上每次减去第一个和第三个值0.1
并将第 28 行从
更改为
image0.bmp
到
image1.bmp
所以我的程序第一次需要 7.0 7.0 7.0 和 image0.bmp
第二次我希望它达到 运行 6.9 7.0 6.9 和 image1.bmp
等等...
任何人都可以给我一些如何完成它的提示吗?
提前致谢!
使用 awk:
#!/bin/bash
# set iter to be the number of times to execute the script
# alternatively use and pass it as parameter to the script
iter=10
for (( i=1; i<=$iter; i++ )); do
awk 'NR==3 {print -0.1, , -0.1; FS="[e.]"}
NR==28 {print "e" +1 "." }
NR!=3 && NR!=28 {print}' params.dat > .tmpfile
mv .tmpfile params.dat
done
awk 代码将到达第 3 行并从第一和第三字段中减去 0.1,默认情况下由 space 分隔。然后它将字段分隔符设置为 'e' 或句点。然后当我们到达第 28 行时,该行被分成三个字段: 'imag' <field separator 'e'> '0' <field separator '.'> 'bmp'
我们增加第二个字段并打印结果。所有其他行将按原样打印。
我有一个名为 params.dat 的数据文件,我想在每次 运行 我的代码时更改文件中的值。
这是我到目前为止得到的
i=7.0
k=0
i=0
while [ $i -lt 10]
do
sed "3s/.*/$j 6.9 $j/" "28s/.*/image$i.bmp/" params.dat
((i++))
((k++))
((j=j-0.1))
done
目标是更改日期文件的第 3 行和第 28 行
7.0 7.0 7.0
到
6.9 7.0 6.9
基本上每次减去第一个和第三个值0.1
并将第 28 行从
更改为image0.bmp
到
image1.bmp
所以我的程序第一次需要 7.0 7.0 7.0 和 image0.bmp
第二次我希望它达到 运行 6.9 7.0 6.9 和 image1.bmp 等等...
任何人都可以给我一些如何完成它的提示吗?
提前致谢!
使用 awk:
#!/bin/bash
# set iter to be the number of times to execute the script
# alternatively use and pass it as parameter to the script
iter=10
for (( i=1; i<=$iter; i++ )); do
awk 'NR==3 {print -0.1, , -0.1; FS="[e.]"}
NR==28 {print "e" +1 "." }
NR!=3 && NR!=28 {print}' params.dat > .tmpfile
mv .tmpfile params.dat
done
awk 代码将到达第 3 行并从第一和第三字段中减去 0.1,默认情况下由 space 分隔。然后它将字段分隔符设置为 'e' 或句点。然后当我们到达第 28 行时,该行被分成三个字段: 'imag' <field separator 'e'> '0' <field separator '.'> 'bmp'
我们增加第二个字段并打印结果。所有其他行将按原样打印。