使用bash脚本查找字符串的行号文件 [英] Using Bash Script to Find Line Number of String in File
本文介绍了使用bash脚本查找字符串的行号文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用bash脚本来查找行号的字符串出现?
How can I use a bash script to find the line number where a string occurs?
例如,如果一个文件看起来像这样,
For example if a file looked like this,
Hello I am Isaiah
This is a line of text.
This is another line of text.
和我跑了脚本查找字符串行这将输出数字2,因为这是第一次出现。
and I ran the script to look for the string "line" it would output the number 2, as it is the first occurance.
推荐答案
由于您的示例只打印字符串的第一次出现的行号,也许你正在寻找:
Given that your example only prints the line number of the first occurrence of the string, perhaps you are looking for:
awk '/line/{ print NR; exit }' input-file
如果你真的想所有的事件(例如,如果你的榜样的期望输出实际上是2 \\ N3 \\ n),省略了退出
。
If you actually want all occurrences (eg, if the desired output of your example is actually "2\n3\n"), omit the exit
.
这篇关于使用bash脚本查找字符串的行号文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文