我必须找到使用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行相同,因为普通文件中有许多带有逗号的行,因此行号实际上应该更少。
这里怎么了?
谢谢。
发布于 2014-02-06 02:52:12
遗憾的是,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 } }'
发布于 2014-02-06 03:01:13
或者你可以试试
grep -v , text.txt | head -250 | tail -1
https://stackoverflow.com/questions/21593038
复制相似问题