Linux ed Editor 一行 search/replaces
Linux ed Editor multiple search/replaces in one line
我想用 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 -
我想把"c0"全部换成"c1"在/dev/ ..
这是我放入 ed 的正则表达式:1,$s/\(\/dev.*\)\(c0\)\(d0s.*\)/c1/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.*\)/c1c1/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" 因为它没有被搜索字符串
所以我得到了一个文件,其中一行有多个搜索结果,但它只替换了一个实例。我知道 "global"-Flag 在文件的第一步运行并标记匹配的 line。我希望我知道是什么导致了这个问题,但我没有足够的 ed 经验来知道如何解决它。
为你加油干杯
您可以使用 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 -
我想用 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 -
我想把"c0"全部换成"c1"在/dev/ ..
这是我放入 ed 的正则表达式:1,$s/\(\/dev.*\)\(c0\)\(d0s.*\)/c1/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.*\)/c1c1/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" 因为它没有被搜索字符串
所以我得到了一个文件,其中一行有多个搜索结果,但它只替换了一个实例。我知道 "global"-Flag 在文件的第一步运行并标记匹配的 line。我希望我知道是什么导致了这个问题,但我没有足够的 ed 经验来知道如何解决它。
为你加油干杯
您可以使用 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 -