Search code examples
sed

Uncomment block of lines in a file


-- *****************************************************************************
-- ** Storage group name                         = IBMSTOGROUP
-- **   Storage group ID                         = 0
-- **   Data tag                                 = None
-- *****************************************************************************
-- SET STOGROUP PATHS FOR IBMSTOGROUP
-- ON '/udb/ss/DI10'
-- ,  '/udb/ss/DI9'
-- ,  '/udb/ss/DI8'
-- ,  '/udb/ss/DI7'
-- ,  '/udb/ss/DI6'
-- ,  '/udb/ss/DI5'
-- ,  '/udb/ss/DI4'
-- ,  '/udb/ss/DI3'
-- ,  '/udb/ss/DI2'
-- ,  '/udb/ss/DI1'
-- ;

Need to replace -- with ''.

I used sed command but it is displaying twice.

 sed -n "/-- ON/,/DI1.*$/c\ON '/udb/ss/DI1','/udb/ss/DI2','/udb/ss/DI3'/" file

I need to replace -- with ''.

Expected Output:

-- *****************************************************************************
-- ** Storage group name                         = IBMSTOGROUP
-- **   Storage group ID                         = 0
-- **   Data tag                                 = None
-- *****************************************************************************
SET STOGROUP PATHS FOR IBMSTOGROUP
ON '/udb/ss/DI10'
,  '/udb/ss/DI9'
,  '/udb/ss/DI8'
,  '/udb/ss/DI7'
,  '/udb/ss/DI6'
,  '/udb/ss/DI5'
,  '/udb/ss/DI4'
,  '/udb/ss/DI3'
,  '/udb/ss/DI2'
,  '/udb/ss/DI1'
;

Solution

  • You may use this sed command to remove comments in a block from SET line to ; line:

    sed -E '/^-- *SET/,/^-- *;/ s/^-- *//' file
    
    -- *****************************************************************************
    -- ** Storage group name                         = IBMSTOGROUP
    -- **   Storage group ID                         = 0
    -- **   Data tag                                 = None
    -- *****************************************************************************
    SET STOGROUP PATHS FOR IBMSTOGROUP
    ON '/udb/ss/DI10'
    ,  '/udb/ss/DI9'
    ,  '/udb/ss/DI8'
    ,  '/udb/ss/DI7'
    ,  '/udb/ss/DI6'
    ,  '/udb/ss/DI5'
    ,  '/udb/ss/DI4'
    ,  '/udb/ss/DI3'
    ,  '/udb/ss/DI2'
    ,  '/udb/ss/DI1'
    ;
    

    Explanation:

    • /^-- *SET/,/^-- *;/: Match a range of lines starting from -- SET to -- ; line
    • s/^-- *//: Remove -- followed by 0 or more spaces at the line start