Search code examples
csh

How to change string between 2 string in csh?


I have input file as below and I wish to substitute string between set xx { \ & } as showed in output. How to code in csh?

Input:

set a 1
set b 2
set xx { \
a/c/d \
apple/d/e/g \
guava/s/s/g/b/c \
}
set c 3

Output:

set a 1
set b 2
set xx { \
a/*c*/d* \
apple/d/*e*/g* \
guava/s/s/g/*b*/c* \
}
set c 3

Solution

  • $ cat fixer.sed
    #!/bin/sed -f
            /set xx {/ , /}/ {
               s@\(/.*\)\([^/]\)\(/[^/]*\)\(  *[\]\)$@\1*\2*\3*\4@
            }
    

    /set xx {/ , /}/ is a range expression, sed scans each line, but only performs the s@...@...@ operations on lines in between the range supplied.

    $ chmod +x fixer.sed  # only the first time you create file
    

    output

    $ ./fixer.sed txt.txt
    set a 1
    set b 2
    set xx { \
    a/*c*/d* \
    apple/d/*e*/g* \
    guava/s/s/g/*b*/c* \
    }
    set c 3