Command substitution within sed expression

前端 未结 4 1079
抹茶落季
抹茶落季 2020-12-22 02:49

I\'m having little problem with bash/sed. I need to be able to use command substitution within sed expression. I have two big text files:

  • first is logfile.t

4条回答
  •  难免孤独
    2020-12-22 03:05

    I don't know if this would work, since I can't get an answer on whether or not capture groups persist, but there is a lot more to sed than just the s command. I was thinking you could use a capture group in a regex line selector, then use that for the command substitution. Something like this:

    /ERRORID:\(0x[0-9a-f]*\)/  s/ERRORID:0x[0-9a-f]*/ERROR:$(grep \1 errors.txt | grep -o '^[A-Z_]*' )/
    

    Anyway, if that doesn't work I would change gears and point out that this is really a good job for Perl. Here's how I would do it, which I think is much cleaner / easier to understand:

    #!/usr/bin/perl
    
    while(<>) {
      while( /ERRORID:(0x[0-9a-f]*)/ ) {
        $name = system("grep $1 errors.txt | grep -o '^[A-Z_]*'");
        s/ERRORID:$1/ERROR:$name/g;
      }
      print;
    }
    

    Then execute:

    ./thatScript.pl logfile.txt
    

提交回复
热议问题