Bash:命令输出中的grep模式 [英] Bash: grep pattern from command output

查看:204
本文介绍了Bash:命令输出中的grep模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对bash真的很陌生,但这是学校的主题之一.练习之一是:

I'm really new with bash, but it's one of the subjects on school. One of the exercises was:

提供文件"/etc/passwd"的行号,有关您自己的登录信息.

假设USERNAME是我自己的登录ID,我可以用这种方式完美地做到这一点:

Suppose USERNAME is my own login ID, I was able to do it perfectly in this way:

 cat /etc/passwd -n | grep USERNAME | cut -f1

只需给出所需的行号(可能有一种更优化的方法).但是,我想知道是否有一种方法可以使该命令更通用,以便它使用whoami的输出来表示grep模式,而不使用脚本或使用变量.换句话说,要使其成为易于阅读的单行命令,如下所示:

Which simply gave the line number required (there may be a more optimised way). I wondered however, if there was a way to make the command more general so that it uses the output of whoami to represent the grep pattern, without scripting or using a variable. In other words, to keep it an easy-to-read one-line command, like so:

 cat /etc/passwd -n | grep (whoami) | cut -f1

对不起,如果这是一个真正的菜鸟问题.

Sorry if this is a really noob question.

推荐答案

cat /etc/passwd -n | grep `whoami` | cut -f1 

将命令括在`标记中使它执行该命令,并将输出发送到包装的命令中.

Surrounding a command in ` marks makes it execute the command and send the output into the command it's wrapped in.

这篇关于Bash:命令输出中的grep模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆