我必须使用 grep 命令找到没有逗号的 txt 文件的第 250 行。该文件有超过 250 行,但我必须找到第 250 行没有。
例子:
Imagine you had a 10 line file and you were looking for the 3rd line that did not contain a comma
Lines 1 and 2 do not have a comma
lines 3-7 have a comma
Line 8 does not contain a comma
The answer for this file would be line 8.
我使用了这个命令:
grep -vn "," test.txt
然后查找第 250 行,但随后我执行了以下命令以查看行号是否相同,结果确实如此。
grep -n "this is text from the 250th line" test.txt
我不认为应该是这种情况,因为文件的第 250 行 没有 不应该与常规文件的第 250 行在同一行,因为常规文件有很多行中有逗号,所以行号实际上应该更少。
这里有什么问题吗?
谢谢。
最佳答案
不幸的是,posix 不同意:
$ man grep
# ...
-n, --line-number
Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)
# ...
你可以做的是将一些 awk
放在:
$ grep -v , text.txt | awk '{ if (NR == 250) { print } }'
关于linux - 使用 grep 命令 Linux 查找文件的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593038/