Home > Net >  Processing sed matching group through another sed command
Processing sed matching group through another sed command

Time:07-15

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?

CodePudding user response:

Using sed

$ sed -E ':a;s~/([^/])(.*:)~\\\1\2~;ta;s/^linuxroot/d:\\sftp/' input_file
d:\sftp\a\b\c\file.c:200:1: error: evil use of / character

CodePudding user response:

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

CodePudding user response:

Hold it, cut it, change it, grab it, mix it [1].

sed '
  h                                 # hold the line in hold space
  s/:.*//                           # remove all after :
  s~/~\\~g                          # replaces slashes
  s/^linuxroot/d:\\sftp/            # replace linuxroot
  G                                 # append hold space
  s/\([^\n]*\)[^:]*\(.*\)/\1\2/     # shuffle pattern space
  '

Is there any mechanism in sed to do something like this?

Yes, with hold space you can implement any kind of functionality, like a stack, where you can hold stuff for grabing later.

  • Related