最后出现尴尬模式的行数
我是Awk命令的新手。我正在尝试一种方法来打印我的模式匹配的最后一行号码。 我需要将该尴尬命令集成到TCL脚本中。 如果有人回答,请告诉我。
exec awk -v search=$var {$0~search{print NR; exit}} file_name
我正在使用它打印第一次出现的行号。
I am new to awk commands. I am trying a way to print the last line number for my pattern match.
I need to integrate that awk command in tcl script..
If someone has answer to it, please let me know.
exec awk -v search=$var {$0~search{print NR; exit}} file_name
I am using this to print the line number of first occurrence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会在此任务上使用
awk
awk 以下,令file.txt
请输出
说明
:我正在寻找包含
12
的最后一行。 ,当遇到此类行时,我将变量的n
的值设置为行数(nr
),当处理所有行线时,iprint> print
所说的价值。(在GAWK 4.2.1中测试)
I would harness GNU
AWK
for this task following, letfile.txt
content bethen
output
Explanation: I am looking for last line containing
12
somewhere, when such line is encountered I set value of variablen
to number of row (NR
), when all lines of lines are processed Iprint
said value.(tested in gawk 4.2.1)
或者,没有
awk
:Or, without
awk
:在您显示的样本和努力下,请尝试以下
TAC
+awk
代码。说明:
tac
命令以从底部到顶部以相反的顺序打印input_file的输出从awk
程序中,稍后解释)。TAC INPUT_FILE
输出到awk
程序作为输入。中awk
程序创建变量命名lines
具有Input_file行的总数,在主程序中检查行是否包含12
然后打印lines-fnr+1 值,并使用
退出
从程序中退出,我们不需要读取整个input_file。With your shown samples and efforts please try following
tac
+awk
code.Explanation:
tac
command to print Input_file's output in reverse order from bottom to top(basically to get very last match very quickly at first and exit fromawk
program, explained later on).tac Input_file
output toawk
program as an input.awk
program creating variable namedlines
which has total number of lines of Input_file and in main program checking if line contains12
then printinglines-FNR+1
value and usingexit
exiting from program then, by which we need NOT to read whole Input_file.