Linux ed编辑器在一行中多次搜索/替换

时间:2017-06-10 18:32:22

标签: regex search replace editor ed

我希望在以下文件中使用Linux中的ed编辑器在一行中进行多次替换:

#device     device      mount       FS  fsck    mount   mount
#to mount   to fsck     point       type    pass    at boot options
#
#/dev/dsk/c0d0s2 /dev/rdsk/c0d0s2 /usr      ufs 1   yes -
fd  -   /dev/fd fd  -   no  -
/proc   -   /proc   proc    -   no  -
/dev/dsk/c0d0s3 -   -   swap    -   no  -
/dev/dsk/c0d0s0 /dev/rdsk/c0d0s0    /   ufs 1   no  -
/dev/dsk/c0d0s4 /dev/rdsk/c0d0s4    /usr    ufs 1   no  -
/dev/dsk/c0d0s1 /dev/rdsk/c0d0s1    /var    ufs 1   yes -
/dev/dsk/c0d0s7 /dev/rdsk/c0d0s7    /export ufs 2   yes -
/dev/dsk/c0d0s5 /dev/rdsk/c0d0s5    /usr/informix   ufs 2   yes -
swap    -   /tmp    tmpfs   -   yes -

我想在 / dev / ....中将所有“c0”“c1”交换....

以下是我的正则表达式:1,\$s/\(\/dev.*\)\(c0\)\(d0s.*\)/\1c1\3/g,其结果为

   #device      device      mount       FS  fsck    mount   mount
    #to mount   to fsck     point       type    pass    at boot options
    #
    #/dev/dsk/c0d0s2 /dev/rdsk/c1d0s2 /usr      ufs 1   yes -
    fd  -   /dev/fd fd  -   no  -
    /proc   -   /proc   proc    -   no  -
    /dev/dsk/c1d0s3 -   -   swap    -   no  -
    /dev/dsk/c0d0s0 /dev/rdsk/c1d0s0    /   ufs 1   no  -
    /dev/dsk/c0d0s4 /dev/rdsk/c1d0s4    /usr    ufs 1   no  -
    /dev/dsk/c0d0s1 /dev/rdsk/c1d0s1    /var    ufs 1   yes -
    /dev/dsk/c0d0s7 /dev/rdsk/c1d0s7    /export ufs 2   yes -
    /dev/dsk/c0d0s5 /dev/rdsk/c1d0s5    /usr/informix   ufs 2   yes -
    swap    -   /tmp    tmpfs   -   yes -  

然而,这个长正则表达式:
1,\$s/\(\/dev.*\)\(c0\)\(d0s.*\)\(\/dev.*\)\(c0\)\(d0s.*\)/\1c1\3\4c1\6/g会产生这样的结果:

#device     device      mount       FS  fsck    mount   mount
#to mount   to fsck     point       type    pass    at boot options
#
#/dev/dsk/c1d0s2 /dev/rdsk/c1d0s2 /usr      ufs 1   yes -
fd  -   /dev/fd fd  -   no  -
/proc   -   /proc   proc    -   no  -
/dev/dsk/c0d0s3 -   -   swap    -   no  -
/dev/dsk/c1d0s0 /dev/rdsk/c1d0s0    /   ufs 1   no  -
/dev/dsk/c1d0s4 /dev/rdsk/c1d0s4    /usr    ufs 1   no  -
/dev/dsk/c1d0s1 /dev/rdsk/c1d0s1    /var    ufs 1   yes -
/dev/dsk/c1d0s7 /dev/rdsk/c1d0s7    /export ufs 2   yes -
/dev/dsk/c1d0s5 /dev/rdsk/c1d0s5    /usr/informix   ufs 2   yes -
swap    -   /tmp    tmpfs   -   yes -

将第一个“c0”保留为“c0”,因为搜索字符串找不到它

所以我得到了一个文件,我在一行中获得了多个搜索结果,但它只替换了一个实例。我知道“全局”-Flag在文件的第一步中运行并标记匹配的。我希望我知道导致这个问题的原因,但是我没有足够的经验知道怎么解决它 干杯为你提供帮助

1 个答案:

答案 0 :(得分:0)

您可以使用g命令搜索带有dev字符串的行,然后使用s(替换)命令和g(全局)标记全局替换:

g/\/dev/s/c0/c1/g

给你预期的结果:

#device     device      mount       FS  fsck    mount   mount
#to mount   to fsck     point       type    pass    at boot options
#
#/dev/dsk/c1d0s2 /dev/rdsk/c1d0s2 /usr      ufs 1   yes -
fd  -   /dev/fd fd  -   no  -
/proc   -   /proc   proc    -   no  -
/dev/dsk/c1d0s3 -   -   swap    -   no  -
/dev/dsk/c1d0s0 /dev/rdsk/c1d0s0    /   ufs 1   no  -
/dev/dsk/c1d0s4 /dev/rdsk/c1d0s4    /usr    ufs 1   no  -
/dev/dsk/c1d0s1 /dev/rdsk/c1d0s1    /var    ufs 1   yes -
/dev/dsk/c1d0s7 /dev/rdsk/c1d0s7    /export ufs 2   yes -
/dev/dsk/c1d0s5 /dev/rdsk/c1d0s5    /usr/informix   ufs 2   yes -
swap    -   /tmp    tmpfs   -   yes -