unix AIX sed -查找完全匹配项并替换

falq053o  于 2022-12-12  发布在  Unix
关注(0)|答案(2)|浏览(250)

I need to use sed on AIX to find and replace exact match of a string.
as an example we have a like like below

LogicalVolume hdisk1 hdisk10

and I have 2 variables

old_disk=hdisk1
new_disk=hdisk50

Here is the sed command I would like to use

sed "s/"$old_disk"/"$new_disk"/" file1.txt \> file2.txt

The outcome that I get looks like below

LogicalVolume hdisk50 hdisk500

instead of

LogicalVolume hdisk50 hdisk10

Unfortunately < and > do not work on AIX and I don't know how to replace the exact match of variable old_disk. I also tried ' and " around the variable, but it doesn't work neither.

mklgxw1f

mklgxw1f1#

假设perl在AIX上默认可用,我建议使用它,以避免使用带有错误数据的磁盘相关命令!

old_disk=hdisk1
new_disk=hdisk50
export old_disk new_disk
perl -pe 's/\b$ENV{"old_disk"}\b/$ENV{"new_disk"}/g' < file1.txt > file2.txt

这将设置与示例相同的两个变量,然后将它们导出到环境中,以便下面的perl命令可以访问它们。perl命令只是搜索任何“old_disk”值并将其替换为“new_disk”值,但通过要求搜索文本的两侧都有单词边界来限制搜索文本。单词边界是单词字符的变化(字母数字和_)转换为非单词字符(反之亦然)。

jaxagkaj

jaxagkaj2#

sed命令 * 可能 *

sed 's/\([[:space:]]\|^\)'"$old_disk"'\([[:space:]]\|$\)/\1'"$new_disk"'\2/' file1.txt > file2.txt

但是我没有AIX环境来进行测试。
它查找old_disk,其前面是空格或行首,后面是空格或行尾。

相关问题