使用grep命令Linux查找文件的行号
我必须使用grep命令找到一个没有逗号的txt文件的第250行。该文件有250多行,但我必须找到第250行没有 例如:使用grep命令Linux查找文件的行号,linux,grep,Linux,Grep,我必须使用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
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 } }'
或者你可以试试
grep -v , text.txt | head -250 | tail -1