查看由数字表示的文件的特定行

XXL*_*XXL 20 unix linux bash file

好吧,这可能是一个显而易见的事情,但它逃脱了我,因为它可能以一种我不知道的更简单的方式完成,到目前为止..说有一个"文件",我想只查看什么是在该文件的行号"X"上,解决方案是什么?

这是我能想到的:

head -X < file | tail -1  
 sed -n Xp < file
Run Code Online (Sandbox Code Playgroud)

unix/gnu/linux text-tools/utils的标准集还有什么(或任何其他方式)吗?

Jmo*_*y38 33

sed -n 'Xp' theFile,X您的行号在哪里,theFile是您的文件.

  • @fig你在说什么?它是如何最好的答案?这张海报并没有费心阅读OP的帖子,**已经**已经有了这个确切的部分.发布相同的东西有什么用?! (2认同)
  • 我很少在问题中寻找答案,因此将其作为单独的答案很有用,可以相对于其他答案进行赞成或反对投票。+1 (2认同)

小智 6

在vi中:

vi +X filename
Run Code Online (Sandbox Code Playgroud)

在EMACS中:

emacs +X filename
Run Code Online (Sandbox Code Playgroud)

在shell中:

nl -ba -nln filename| grep '^X '
Run Code Online (Sandbox Code Playgroud)

您可以使用上下文grep cgrep而不是grep查看匹配行上方和下方的某些行.


例子:

只打印一行:

$ nl -ba  -nln  active_record.rb  | grep '^111 '
111       module ConnectionAdapters
Run Code Online (Sandbox Code Playgroud)

与上下文:

$ nl -ba  -nln  active_record.rb  | grep -C 2 '^111 '
109       end
110     
111       module ConnectionAdapters
112         extend ActiveSupport::Autoload
113     
Run Code Online (Sandbox Code Playgroud)

用于grep检查中的上下文控制man grep:

   Context Line Control
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -C NUM, -NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.
Run Code Online (Sandbox Code Playgroud)


pep*_*uan 4

awk 一行:

awk "NR==$X" file
Run Code Online (Sandbox Code Playgroud)

bash 循环:

for ((i=1; i<=X; i++)); do
  read l
done < file
echo "$l"
Run Code Online (Sandbox Code Playgroud)