0

我有以下情况。

 awk '$0 != "Force for tool binder"   # print all lines until string found
 $0 =="Force for tool binder"{
 print ; getline ; print;            # go to 2 lines below the string
 getline; getline < " forceState$j.k "; print}' dynaFile_Offen1.k > tempDynaFile.k   # take the string from 
 #the file forceStates$j.k and replace in the main file, generating a temp file.

问题是这里的 j 是一个循环索引,这意味着对于第一种情况它是 j=1。当我将它用作 forceStates1.k 时,它工作得很好,但在循环中它没有取值。

我将不得不接受这些建议。

4

2 回答 2

0

$jin'... " forceState$j.k " ...'不会被扩展。
这是个问题吗?

于 2012-03-06T15:30:28.367 回答
0

$j 看起来是一个 shell 脚本变量。由于您的 awk 脚本位于单引号部分中,因此 shell 不会替换 awk 脚本中的变量。所以 awk 看到的是文字$j。您需要更改 shell 引用以允许替换,或者可能更有用地将其在命令行中传递,例如:

awk -v loopctr=$j 'BEGIN {print loopctr}'

每次都会打印你的循环计数器,因为这里j的 awk 变量中传递了 的值loopctr

于 2012-03-06T15:44:19.230 回答