想要精确匹配字符串(尽管有变体),并仅删除该字符串

inf*_*cal 1 language-agnostic string

我需要从文件中删除特定的、精确的字符串。这被用作我正在实施的清理过程的一部分。问题是,有些变体与我要删除的特别精确的字符串相似,但不完全相同。

例如,以下是文件“sample”的示例:

tmp2
tmp3
tmp0
tmp1
tmp3
tmp3
tmp3
tmp1.1
tmp3
tmp2
tmp3
tmp1.2
tmp4
Run Code Online (Sandbox Code Playgroud)

我只想删除“tmp1”,而不是“tmp1.1”或“tmp1.2”。

我正在使用单行 Perl 命令:

perl -i -nle 'print if !/tmp1/' ./sample
Run Code Online (Sandbox Code Playgroud)

显然,单行脚本并不流畅。当然,它会删除“tmp1”,但是,它也会删除“tmp1.1”和“tmp1.2”。

有任何想法吗?

Bor*_*ork 6

使用锚点。^用于行首和$行尾。

$ perl -i -nle 'print if !/^tmp1$/' ./sample
Run Code Online (Sandbox Code Playgroud)