如何检索在bash命令输出的第一个字? [英] How to retrieve the first word of the output of a command in bash?

查看:78
本文介绍了如何检索在bash命令输出的第一个字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令,例如:回声字1字词2。我想提出一个管道(|),并从命令获取字1

I have a command, for example: echo "word1 word2". I want to put a pipe (|) and get word1 from the command.

回声字词1字词2| ....

我不知道管后放什么。

推荐答案

awk中是一个不错的选择,如果你要处理尾随空白,因为它会照顾它给你:

Awk is a good option if you have to deal with trailing whitespace because it'll take care of it for you:

echo "   word1  word2 " | awk '{print $1;}' // Prints "word1"

切不会采取这虽然照顾:

Cut won't take care of this though:

echo "  word1  word2 " | cut -f 1 -d " " // Prints nothing/whitespace

剪切这里什么都不打印/空格,因为在空格前的第一件事就是另一个空间。

'cut' here prints nothing/whitespace, because the first thing before a space was another space.

这篇关于如何检索在bash命令输出的第一个字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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