nawk 并不难理解,并且和其他语言一样,我猜你无法理解它,因为它没有正确格式化,如果你格式化它,你就会知道它是如何工作的。
要回答您的问题,此命令将在给定输入文件中搜索包含输入文本的行,并在匹配行之前打印几行,在匹配行之后打印几行。要打印的行数由变量“b”(之前的行数)和“a”(之后的行数)控制,要搜索的字符串/文本使用变量“s”传递。
此命令将有助于调试/故障排除,其中希望通过搜索一些错误文本并在其上方打印一些行并在其后打印一些行来从大型日志文件(难以在 vi 或 UNIX/LINUX 上的其他编辑器中打开)中提取行.
所以在你的指挥下
b=1 ## means print only 1 line before the matching line
a=19 ## means print 19 lines after the matching line
s="<Comment>Ericsson_OCS_V1_0.0.0.7" ## means search for this string
/var/opt/fds/config/ServiceConfig/ServiceConfig.cfg ## search in this file
/opt/temp/"$circle"_"$sdpid"_RG.cfg ## store the output in this file
您的格式化命令如下,在格式化之前看起来像 c-->0 的第一个条件很容易解释,这意味着 c-- 大于 0。AWK 中的 NR 变量给出了输入文件中当前处理行的行号处理。
nawk '
c-- > 0;
$0 ~ s
{
if(b)
for(c=b+1;c>1;c--)
print r[(NR-c+1)%b];
print;
c=a
}
b
{
r[NR%b]=$0
}' b=1 a=19 s="<Comment>Ericsson_OCS_V1_0.0.0.7" /var/opt/fds/config/ServiceConfig/ServiceConfig.cfg > /opt/temp/"$circle"_"$sdpid"_RG.cfg