Friday, July 29, 2022

[SOLVED] Processing sed matching group through another sed command

Issue

I'd like to process the output of a compile done in linux, and convert all error references to use dos file paths which can be parsed by a windows IDE. So, for example if I had the following line in the unix file:

linuxroot/a/b/c/file.c:200:1: error: evil use of / character 

I'd like it to output:

d:\sftp\a\b\c\file.c:200:1: error: evil use of / character

So basically, I want to find anything that matches: ^\(linuxroot/[A-Za-z0-9_/.-]*\):[0-9]*:[0-9]*: error: \(.*\)$, and then run only the first group through another sed replacement: sed -e "s|\/|\\\\|g" -e "s|^\\\\linuxroot|d:\\\\sftp". Is there any mechanism in sed to do something like this?


Solution

I've come up with this:

$ cat file
linuxroot/aaaaa/file.c:200:1: error: evil use of / character
linuxroot/a/bbb/file.c:200:1: error: evil use of / character
linuxroot/a/b/c/file.c:200:1: error: evil use of / character
$ cat file.sed
\!^linuxroot/[A-Za-z0-9_/.-]*:[0-9]*:[0-9]*: error: .*! {
:loop
    # try to replace a '/' to the left of ':'
  s!^([^:]*)/([^:]*)!\1\\\2!
    # if succeeded, try again
  tloop
    # now there's no '/' to the left of ':'
  s!^linuxroot!d:\\sftp!
}
$ sed -E -f file.sed file
d:\sftp\aaaaa\file.c:200:1: error: evil use of / character
d:\sftp\a\bbb\file.c:200:1: error: evil use of / character
d:\sftp\a\b\c\file.c:200:1: error: evil use of / character


Answered By - pynexj
Answer Checked By - Willingham (WPSolving Volunteer)