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
是您的文件.
小智 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)
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)