如何使用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:\test
但是,我希望仅从每行中提取名称(即"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屋!
查看全文