Find all instances of word occurring in a file

让人想犯罪 __ 提交于 2019-12-22 18:31:56

问题


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

My output would be as follows:

   $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

回答1:


You can just use grep with right regex:

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

UPDATE: As per comments:

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

test
234


来源:https://stackoverflow.com/questions/20240627/find-all-instances-of-word-occurring-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!