1

I am using notepad++, I have wrote few regex which have eased my work in formatting file. However to format the file in desired format I have to execute multiple regex, I wanted to know is there some possible way where I will store my list of regular expression and it will execute one after another.

Here is the file tree.txt which needs to be formatted.

overview/Main_Overview/Gas Management/BOG MV/BOG MV Process Alarms:Tag=BOG Heaters Process Alarms:Device=:Color=-3355444
overview/Main_Overview/Gas Management/BOG MV/BOG MV Process Trip:Tag=BOG Heaters Process Alarms-1:Device=:Color=-3355444
overview/Main_Overview/Gas Management/BOG MV/BOG MV External Trip:Tag=BOG Heaters Process Alarms-1-1:Device=:Color=-3355444

The regex will truncate above text and arrange in this sed format.

s/BOG Heaters Process Alarms:/BOG MV Process Alarms:/g
s/BOG Heaters Process Alarms-1:/BOG MV Process Trip:/g
s/BOG Heaters Process Alarms-1-1:/BOG MV External Trip:/g

To achieve the above result I will do some set of regex operation one after another.

Find- .*/
Replace- 

Find- Device=:.*
Replace- 

Find- :Tag=
Replace- /

Find- (^.*/)([^/]+):
Replace- /\2/ \1

Find- ^
Replace- s

Find- $
Replace- g

After above regex I get the desired format however I need to know if this can be combined so that I don't need to do manually after each expression gets completed or reduce to one expression. Thanks

1

1 Answer 1

0

You already have the elements you need, especially grouping. You need to find:

^.*/(.*:)Tag=(.*:)Device=.*$

Then replace with:

s/\2/\1/g

Note that this gives the sed parameters you quote as what you want to produce, whereas your sequence would omit the colon in the sed find string and add a leading blank in the sed replacement string, giving:-

s/BOG Heaters Process Alarms/ BOG MV Process Alarms:/g
s/BOG Heaters Process Alarms-1/ BOG MV Process Trip:/g
s/BOG Heaters Process Alarms-1-1/ BOG MV External Trip:/g

To reproduce your sequence you would need to search for:

^.*/(.*):Tag=(.*:)Device=.*$

Then replace with:

s/\2/ \1/g

However, the sed substitutions won't work unless made in reverse order.

1
  • Thanks it worked, I didn't knew we could group this way.
    – gadhvi
    Commented May 24, 2018 at 4:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .