如何使用 grep 从行中提取特定字段值 [英] How do I use grep to extract a specific field value from lines
本文介绍了如何使用 grep 从行中提取特定字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件中有如下几行
....... DisplayName="john" ..........
其中 ....
表示其他字段的可变数量.
where ....
represents variable number of other fields.
使用以下 grep 命令,我能够提取具有有效DisplayName"字段的所有行:
Using the following grep command, I am able to extract all the lines which have a valid 'DisplayName' field:
grep DisplayName="[0-9A-Za-z[:space:]]*" e: est
但是,我希望只从每一行中提取名称(即john"),而不是从 grep 返回的整行中提取.我尝试将输出传送到 cut
命令中,但它不接受字符串分隔符.
However, I wish to extract just the name (ie "john") from each line instead of the whole line returned by grep. I tried piping the output into the cut
command but it does not accept string delimiters.
推荐答案
这对我有用:
awk -F "=" '/DisplayName/ {print $2}'
返回 "john"
.要删除 john
的引号,请使用:
which returns "john"
. To remove the quotes for john
use:
awk -F "=" '/DisplayName/ {gsub(""","");print $2}'
这篇关于如何使用 grep 从行中提取特定字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文