gpt4 book ai didi

linux - 查找文件中出现的所有单词实例

转载 作者:太空狗 更新时间:2023-10-29 11:45:09 26 4
gpt4 key购买 nike

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

例如,如果这是我的文件

测试.conf

$temp_test
test
$1234_$temp
$temp_234

我的输出如下:

   $temp_test
$temp_234

谁能告诉我这怎么可能?这是迄今为止我能得到的最接近的结果。

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

最佳答案

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

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

关于linux - 查找文件中出现的所有单词实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com