【发布时间】:2013-04-05 17:43:40
【问题描述】:
这里是新手。我正在尝试使用 awk 打印一些信息。于是我写了一个shell脚本
#!/bin/bash
turbVar="U"
bcName="outlet"
str="$turbVar $bcName b.c. = "
# method-1
awk -v RS='}' '/'$bcName'/ { printf "%20s = %s\n" $str $4; exit;}' BCFile | tr -d ";"
# method-2
awk -v RS='}' -v var1=$bcName '$0 ~ var1 { printf "%20s = %s\n" $str $4; exit;}' BCFile | tr -d ";"
BCFile 文件内容是
boundary
{
inlet
{
type fixedValue;
value uniform (5 0 0);
}
outlet
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
....
}
我希望输出类似的东西
U outlet b.c. = inletOutlet
很遗憾,这不起作用。它抱怨awk: (FILENAME=0/U FNR=4) fatal: not enough arguments to satisfy format string %20s = %s。
为什么我不能在 awk printf 中使用 $str 变量?
第二个问题,哪种方法更好?使用'/'$bcName'/或者-v var1=$bcName '$0 ~ var1?,为什么我不能直接使用'/$bcName/或者'/"$bcName"/?这里的强引用和弱引用有什么区别?
【问题讨论】:
-
0/U应该是什么? -
这是一个文件。它的内容显示在这篇帖子
http://stackoverflow.com/questions/15825150/how-to-find-the-multiline-pattern-match-they-must-be-first-time-match -
文件
U位于名为0的文件夹中。 :) -
@sudo_O - 仅供参考,您不能命名包含正斜杠的文件。 nul 字符“\0”也是如此。
-
@Daniel - 写下您对
What is the difference between strong quote and weak quote的问题。您认为“强”与“弱”引用是什么?我们通常只谈论单引号和双引号。就您的脚本而言,不要执行您问题中提到的任何替代方案 - 它们都非常糟糕.