1

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.

5
  • 1
    please provide the sample output Commented Jul 10, 2023 at 5:25
  • I am trying to replace that line with $muCH4_1 !Methane, where $muCH4_1 is a variable the shell script takes from a different .dat file Commented Jul 10, 2023 at 5:29
  • please update your question and provide your script tested Commented Jul 10, 2023 at 5:34
  • Do you want $muCH4_1 to appear in the output or do you want a value that the variable $muCH4_1 holds to be in the output? Please show the expected output Commented Jul 10, 2023 at 5:38
  • The value that is held in '$muCH4_1' would appear in the output, not the name of the variable. The output would be the same format as in the current snippet of the .dat file, it would just be a different value Commented Jul 10, 2023 at 5:42

1 Answer 1

2

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"' \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")"' \1/' "$file"
Sign up to request clarification or add additional context in comments.

2 Comments

@jlan160 Oups, I accidentally made it -iE instead of -Ei, try now :-)
Yes, this worked, thank you! One issue that also may have contributed was that there is a single space before each number in the .dat file, which I needed to delete in order for this to work, but it solved the problem, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.