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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5418908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:23:47  来源:igfitidea点击:

view a particular line of a file denoted by a number

linuxbashfileunix

提问by XXL

Okay, this is probably an evident thing but it escapes me, as in it could probably be done in a much simpler way that I'm not aware of, so far.. Say there's a "file" and I want to view only what's on line number "X" of that file, what would be the solution?

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

here's what i can think of:

这是我能想到的:

head -X < file | tail -1  
 sed -n Xp < file

is there anything else (or any other way) from the standard set of unix/gnu/linux text-tools/utils?

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

回答by pepoluan

awk one-liner:

awk 单行:

awk "NR==$X" file

bash loop:

bash循环:

for ((i=1; i<=X; i++)); do
  read l
done < file
echo "$l"

回答by Jmoney38

sed -n 'Xp' theFile, where Xis your line number and theFileis your file.

sed -n 'Xp' theFileX你的行号在哪里,theFile你的文件在哪里。

回答by Krrl

Just use vi

只需使用vi

vi file

When in the file type

当在文件类型

:X

where X is the line number you want to see

其中 X 是您要查看的行号

However, the sed -n Xp file is a good way if you really only want to see the one line

但是,如果您真的只想查看一行,sed -n Xp 文件是一个好方法

回答by Krrl

in vi:

在六:

vi +X filename

in EMACS:

在 EMACS 中:

emacs +X filename

in the shell:

在外壳中:

nl -ba -nln filename| grep '^X '

you can use context grep cgrep instead of grepto see some lines above and below the matching line..

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



EXAMPLES:

例子:

print just that one line:

只打印那一行:

$ nl -ba  -nln  active_record.rb  | grep '^111 '
111       module ConnectionAdapters

with context:

有上下文:

$ nl -ba  -nln  active_record.rb  | grep -C 2 '^111 '
109       end
110     
111       module ConnectionAdapters
112         extend ActiveSupport::Autoload
113     

for context control in grepcheck man grep:

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.