bash错误sed: -e表达式#1,char 3: unknown命令:' /'

时间:2022-08-25 00:24:18

I've write this script bash that must write a file with a specifical lines whit different numbers (i.e.: "9.0E-8" ... ) so i must have the last file, after the for cycle, with "9.0E-11" respect "9.0E-7".

我已经编写了这个脚本bash,它必须用一个特殊的行来编写一个文件,其中有不同的数字。:“9.0 e-8”……)所以我必须有最后一个文件,在for循环之后,用“9.0 -11”表示“9.0 -7”。

#!/bin/sh

touch IC_masslessPlanetesimal.java

for n in "9.0E-8" "9.0E-8" "9.0E-9" "9.0E-10" "9.0E-11"
do  
sed -i "21s/9\.0E-7/$n/" IC_masslessPlanetesimal.java
javac IC_masslessPlanetesimal.java
java IC_masslessPlanetesimal
done

Now the script must re-edit, compile and run the file.java and the code run without errors. The problem now is: when i look into the file.java at the 21-th lines (there is the number the scrpt must changes) the scrpt edit only the first for-cycle because when it try to replace AGAIN the string whit the others one ("9.0E-9" etc...) the script can't find the last one !

现在,脚本必须重新编辑、编译和运行该文件。java和代码运行时没有错误。现在的问题是:当我查看文件时。java在第21行(有一个编号,它必须更改),它只编辑第一个for循环,因为当它试图再次替换其他的字符串时(“9.0—9”等等),脚本不能找到最后一个!

first for step) 9e-7 -> 9e-8: script find 9e-7 and replace with n=9e-8 second for step) 9e-8 -> 9e-9: script DOESN'T FIND the 9e-8 string and can't change it !!!

第一步)9e-7 -> 9e-8:脚本找到9e-7,然后用n=9e-8秒代替步骤)9e-8 -> 9e-9:脚本找不到9e-8字串,无法更改!!!

So i must rewrite the for option to incorporate this modification.

所以我必须重写这个选项来合并这个修改。

Any guess please? :(

任何想吗?:(

2 个解决方案

#1


2  

The syntax for sed commands is

sed命令的语法是。

[ address(es) ] command

21 is interpreted as the address, i.e. the line number. / is therefore treated as the command, hence the error.

21被解释为地址,即行号。/因此被视为命令,因此错误。

What are you trying to do? If you want to replace 9.0e7 by $n on line 21, use

你想做什么?如果您想在第21行中以$n替换9.0e7,请使用。

21s/9\.0E7/$n/

#2


2  

Error is here:

错误:

sed -i "21/9.0E7/$n" 

Since you haven't provided any sed command after 21.

因为你21岁以后还没有提供任何sed命令。

Did you forget s (substitute switch) in your sed command?

你在sed命令中忘记了s(替换开关)吗?

It should probably be:

它应该是:

sed -i "s/9.0E7/$n/" 

OR may be this:

或者可能是这样的:

sed -i "21s/9.0E7/$n/" 

#1


2  

The syntax for sed commands is

sed命令的语法是。

[ address(es) ] command

21 is interpreted as the address, i.e. the line number. / is therefore treated as the command, hence the error.

21被解释为地址,即行号。/因此被视为命令,因此错误。

What are you trying to do? If you want to replace 9.0e7 by $n on line 21, use

你想做什么?如果您想在第21行中以$n替换9.0e7,请使用。

21s/9\.0E7/$n/

#2


2  

Error is here:

错误:

sed -i "21/9.0E7/$n" 

Since you haven't provided any sed command after 21.

因为你21岁以后还没有提供任何sed命令。

Did you forget s (substitute switch) in your sed command?

你在sed命令中忘记了s(替换开关)吗?

It should probably be:

它应该是:

sed -i "s/9.0E7/$n/" 

OR may be this:

或者可能是这样的:

sed -i "21s/9.0E7/$n/"