查找文件中出现的所有单词实例 [英] Find all instances of word occurring in a file

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

问题描述

使用 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.

例如,如果这是我的文件

For example if this was my file

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

推荐答案

你可以使用 grep 和正确的正则表达式:

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天全站免登陆