在文件中查找键并输出相应的值

时间:2013-03-22 07:46:51

标签: bash

我的文件file1.txt包含以下内容

P0000021=Result of string 21
P0000022=Result of string 22
P0000023=Result of string 23
P0000024=Result of string 24
P0000025=Result of string 25
P0000026=Result of string 26
P0000027=Result of string 27
T1000028=Result of string 28

并希望使用一些命令来查找任意键并获取相应的值。例如。如果我搜索P0000024,则输出应为

Result of string 24

我尝试使用grep这样:

grep '"P0000024"=' file1.txt | cut -d'=' -f2 > result.txt

然而,它没有给出预期的结果。

3 个答案:

答案 0 :(得分:1)

使用awk:

awk -F= -v x="P0000024" '$1==x{print $2}' file

答案 1 :(得分:1)

使用sed

sed -n '/P0000024=/{s///;p;q}' file

答案 2 :(得分:0)

从grep表达式中删除双引号

grep 'P0000024=' file1.txt | cut -d'=' -f2 > result.txt
相关问题