regexsearchreplaceeditored

Linux ed Editor multiple search/replaces in one line


I want to do multiple replaces in one line with the ed editor in Linux in the following file:

#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 -

I want to swap all "c0" with "c1" in /dev/....

Here is my regex which I put into ed: 1,\$s/\(\/dev.*\)\(c0\)\(d0s.*\)/\1c1\3/g which results in

   #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 -  

However, this long regex:
1,\$s/\(\/dev.*\)\(c0\)\(d0s.*\)\(\/dev.*\)\(c0\)\(d0s.*\)/\1c1\3\4c1\6/g results in this:

#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 -

leaving the first "c0" still as "c0" because it's not found by the search string


So I got a file where I got multiple search results in one line but it only replaces one instance. I know that the "global"-Flag runs in the first step through the file and marks the matching line. I hope I know what causes the problem but i don't have enough experience with the ed to know how to fix it.
Cheers for you help


Solution

  • You can use the g command to search lines with the dev string then replace globally using the s (substitute) command and the g (global) flag :

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

    giving you the expected result:

    #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 -