我是Linux新手,正在尝试基本的终端命令。我发现可以使用compgen -u
列出所有用户,但是如果我只想显示底线输出怎么办?
好吧,可以说compgen -u
的输出如下:
extra
extra
extra
extra
extra
extra
extra
extra
extra
John
William
Kate
Harold
我只能使用grep查找单个文本(例如compgen -u | grep John
)。但是,如果我想使用grep来显示John及其后面的所有其余条目,怎么办?
答案 0 :(得分:1)
sed
或awk
解决方案会更容易,但是如果您只能使用grep
,则选项--after-context
(或-A
)可以做到:
grep -A 5 John file
缺点是您需要知道匹配后要显示的行数(或为文件的其余部分使用任意大数)。
答案 1 :(得分:0)
compgen -u | grep -A$(compgen -u| wc -l) John
说明:
From man grep
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between
contiguous groups of matches.
grep -A -- print number of rows after pattern
$() -- Execute unix command
compgen -u| wc -l --> Get total number of rows of output of command.
答案 2 :(得分:0)
您可以使用以下单线:
n=$( compgen -u | grep -n John | head -1 | cut -d ":" -f 1 ) && compgen -u | tail -n +$n
找出第一次出现John的行号,并打印从该行开始的所有内容。