Find all instances of word occurring in a file

2019-05-27 00:47发布

问题:

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