如何使用sed在shell脚本中修改变量

huangapple go评论57阅读模式
英文:

How to alter a variable with a shell script using sed

问题

我正在尝试修改一个我正在运行的模型的.dat文件中的值。问题在于不能简单地使用sed -i 's/old_text/new_text/'来更改代码。

我正在尝试更改的.dat文件如下:

0.000E-00          !Argon
7.956E-03          !Methane
0.000E-00          !Ethane
1.945E-03          !Carbon Dioxide
9.901E-01          !Nitrogen
1.000E-40          !Oxygen
0.000E-00          !Hydrogen
0.000E-00          !Nitrogen Dioxide
22                 !Tropopause layer

我正在尝试更改甲烷(Methane)、氮气(Nitrogen)和二氧化碳(Carbon Dioxide)的值,但我不能简单地告诉它替换'7.965E-03 !Methane',因为在我第一次运行shell脚本后,该值将发生变化。

我已经尝试将!Methane替换为$'变量名称' !Methane,但这并不能解决删除数字的问题,并且在我编译脚本后的第一次运行后也无法正常工作。

当前的脚本如下:

#file=atmos-master-copy1/CLIMA/IO/mixing_ratios.dat
#sed -i 's/Methane.*/'$muCH4_1'    !Methane/' $file

但我不知道如何使它从文件开头开始更改内容。

英文:

I am attempting to alter the values in a .dat file for a model I am running. The issue is that the code cannot simply be changed with the sed -i 's/old_text/new_text/'

The .dat file I am attempting to change looks like

0.000E-00          !Argon
7.956E-03          !Methane
0.000E-00          !Ethane
1.945E-03          !Carbon Dioxide
9.901E-01          !Nitrogen
1.000E-40          !Oxygen
0.000E-00          !Hydrogen
0.000E-00          !Nitrogen Dioxide
22                 !Tropopause layer

I am attempting to alter the values for methane, nitrogen, and carbon dioxide, however I cannot simply tell it to replace '7.965E-03 !Methane' because after the first time I run the shell script, that value will change.

I have attempted to replace !Methane with $'variable name' !Methane, but that does not solve the issue of removing the number, nor will it work after the first time I compile the script.

The current script is

#file=atmos-master-copy1/CLIMA/IO/mixing_ratios.dat                                                                                        
#sed -i 's/Methane.*/'$muCH4_1'    !Methane/' $file

But I dont know how to make it start by altering the contents at the beginning of the file.

答案1

得分: 2

如果您想用变量$muCH4_1中的值替换!Methane的值,那么:

sed -Ei 's/^\S+\s+(!Methane)/'"$muCH4_1"'/' "$file"
  • ^ - 行的开头锚点
  • \S+ - 一个或多个非空白字符
  • \s+ - 一个或多个空白字符
  • (!Methane) - 捕获!Methane到捕获组1
  • $ - 行的结尾锚点

替换为

  • '"$muCH4_1"' - 变量muCH4_1中的值
  • - 一个字面上的空格
  • \1 - 来自捕获组1的!Methane

注意:为了使第二列与原始文件中的格式保持一致,muCH4_1必须在右侧填充空格。如果这是您想要的效果,您可以使用printf来进行填充:

sed -Ei 's/^\S+\s+(!Methane)/'"$(printf "%-18s" "$muCH4_1")"'/' "$file"
英文:

If you want to replace the value for !Methane with the value held by the variable $muCH4_1, then:

sed -Ei 's/^\S+\s+(!Methane)$/'"$muCH4_1"' /' "$file"
  • ^ - Start of line anchor
  • \S+ - One or more non-whitespace characters
  • \s+ - One or more whitespace characters
  • (!Methane) - Capture !Methane into capture group 1
  • $ - End of line anchor

Substitute with

  • '"$muCH4_1"' - The value held by the variable muCH4_1
  • - A literal space
  • \1 - !Methane from capture group 1

Note: For the second column to line up just like in the original file, muCH4_1 must be padded with spaces to the right. If that's what you want, you can use printf to do the padding:

sed -Ei 's/^\S+\s+(!Methane)$/'"$(printf "%-18s" "$muCH4_1")"' /' "$file"

huangapple
  • 本文由 发表于 2023年7月10日 13:18:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定