我必须使用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行相同,因为常规文件有很多其中包含逗号的行,因此行号实际上应该更少。
这里有什么问题?
谢谢。
答案 0 :(得分:1)
不幸的是,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 } }'
答案 1 :(得分:0)
或者你可以试试
grep -v , text.txt | head -250 | tail -1