如何从正在运行的Node脚本中将参数传递给awk命令?

时间:2022-03-05 23:48:55

The following Javascript code doesn't pass a command string correctly:

以下Javascript代码未正确传递命令字符串:

cp = require("child_process");
var command = "awk < " + mypath + "\" \$3 ~ \"^rs\" {print \$3}\""
cp.exec(command, function (err, stdout, stderr) {});

Is there a more appropriate Node package or Javascript workaround for passing quotation marks and dollar signs to an Awk interpreter that gets invoked in a Bash environment when using child_process in Node?

是否有更合适的Node包或Javascript解决方法,用于将引号和美元符号传递给在Node中使用child_process时在Bash环境中调用的Awk解释器?

2 个解决方案

#1


2  

There is a native-Node awk package, consider using this instead.

有一个native-node awk包,请考虑使用它。

#2


0  

Hmm, if I am reading this correctly:

嗯,如果我正确读到这个:

"awk < " + mypath + "\" \$3 ~ \"^rs\" {print \$3}\""

“awk <”+ mypath +“\”\ $ 3~ \“^ rs \”{print \ $ 3} \“”

it suggests that your parameter order may need tweaking, i.e. start with simple awk:

它表明您的参数顺序可能需要调整,即从简单的awk开始:

awk '{print $0}' /some_file # print each line 

Taking the above (not clear) but do you mean something like:

采取上述(不清楚),但你的意思是:

awk '/^rs/ {print $3}' /some_file ##  

# for each line starting with 'rs' print 3rd variable?

#为每行以'rs'打印第3个变量?

Perhaps something like:

也许是这样的:

Ok, modified a bit; since I'm not familiar with the solution that you are 'calling from', then you will need to tweak appropriately.

好的,修改了一下;因为我不熟悉你“来自”的解决方案,所以你需要适当调整。

"awk '/^rs/{print \$3}'" + "mypath"  ## ?

Or, perhaps (not sure if you need to escape $3):

或者,也许(不确定你是否需要逃避3美元):

"awk '/^rs/{print $3}'" + "mypath"  ## ?

#1


2  

There is a native-Node awk package, consider using this instead.

有一个native-node awk包,请考虑使用它。

#2


0  

Hmm, if I am reading this correctly:

嗯,如果我正确读到这个:

"awk < " + mypath + "\" \$3 ~ \"^rs\" {print \$3}\""

“awk <”+ mypath +“\”\ $ 3~ \“^ rs \”{print \ $ 3} \“”

it suggests that your parameter order may need tweaking, i.e. start with simple awk:

它表明您的参数顺序可能需要调整,即从简单的awk开始:

awk '{print $0}' /some_file # print each line 

Taking the above (not clear) but do you mean something like:

采取上述(不清楚),但你的意思是:

awk '/^rs/ {print $3}' /some_file ##  

# for each line starting with 'rs' print 3rd variable?

#为每行以'rs'打印第3个变量?

Perhaps something like:

也许是这样的:

Ok, modified a bit; since I'm not familiar with the solution that you are 'calling from', then you will need to tweak appropriately.

好的,修改了一下;因为我不熟悉你“来自”的解决方案,所以你需要适当调整。

"awk '/^rs/{print \$3}'" + "mypath"  ## ?

Or, perhaps (not sure if you need to escape $3):

或者,也许(不确定你是否需要逃避3美元):

"awk '/^rs/{print $3}'" + "mypath"  ## ?