[英]Unix find line number of a string in a file using awk/grip
I'm trying to find a position of a string 我试图找到一个字符串的位置
awk -F : '{if ( $0 ~ /Red Car/) print $0}' /var/lab/lab2/rusiuot/stud2001 | tail -l
and somehow I need to find a line position of Red Car. 而且我需要找到红色汽车的行位置。 It is possible to do that using awk or grep? 可以使用awk或grep吗?
You can do 你可以做
awk '/Red Car/ {print NR}' /var/lab/lab2/rusiuot/stud2001
This will print the line number for the line with Red Car
这将为Red Car
打印该行的行号
If you like the line number to be printed at end of the file: 如果您希望在文件末尾打印行号:
awk '/Red Car/ {a[NR]} 1; END {print "\nlines with pattern";for (i in a) printf "%s ",i;print ""}' file
Try something like: 尝试类似:
grep -n "Red Car" /var/lab/lab2/rusiuot/stud2001 | cut -d":" -f 1
-n option will display the line number along with line where pattern is found. -n选项将显示行号以及找到模式的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.