Command substitution within sed expression

前端 未结 4 1072
抹茶落季
抹茶落季 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:23

    With GNU awk for gensub() and the 3rg arg to match():

    $ awk '
        NR==FNR {
            map[$NF] = gensub(/,[^,]+$/,"",1)
            next
        }
        match($0,/(.*ERRORID:)(0x[[:xdigit:]]+)(.*)/,a) {
            $0 = a[1] (a[2] in map ? map[a[2]] : a[2]) a[3]
        }
    1' errors.txt logfile.txt
    Lot's of meaningless stuff ERRORID:LONG_ERROR_DESCRIPTION and something else =>
    

    The above will run much faster than the sed scripts in the currently accepted answer and won't fail given various possible contents of LONG_ERROR_DESCRIPTION such as % or & or \1, nor will it fail when a given ERRORID is a subset of another, e.g. if 0xdead and 0xdeadbeef are 2 separate error codes then the sed scripts can fail depending on the order they appear in errors.txt, e.g. they could convert ERRORS:0xdeadbeef to ERRORS:LONG_ERROR_DESCRIPTIONbeef. by mapping 0xdead first.

提交回复
热议问题