查找文件中发生的所有字词实例 [英] Find all instances of word occurring in a file

查看:179
本文介绍了查找文件中发生的所有字词实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用bash我想知道如何找到一个以文本开头的单词的所有实例,将其存储为一个变量并打印出来。

Using bash I was wondering how I can find all instances of a word which starts with text, store it as a variable and print it out.

例如这是我的文件

test.conf

$temp_test
test
$1234_$temp
$temp_234

我的输出如下:

   $temp_test
   $temp_234

任何人都可以告诉我这是怎么可能的?这是最接近我可以得到的。

Can anyone tell me how this might be possible? This is the closest I could get so far.

while read NAME
do
    echo "$NAME"
done < test.conf


推荐答案

您可以使用正确的正则表达式:

You can just use grep with right regex:

grep '^ *$temp' test.conf
$temp_test
$temp_234

更新:根据评论:

while read -r l; do
   echo "$l"
done < <(sed -n '/^ *$temp_/s/^ *\$temp_//p' t.conf)

test
234

这篇关于查找文件中发生的所有字词实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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