如何用文件中的新单词替换每个逗号

时间:2017-05-23 15:41:20

标签: bash

我有一个日志文件samplelog.log

 [java] 08 May 2017 21:17:37,508  INFO FoobarTests.testSomething.95e0dc44-c51e-4b2f-a048-eba70eae6d94-60e04794-2fe8-42c6-b8a3-5ea180a20431 aaaa.aaa.test.:170 - Creating account: aaaa.aaaa.test.client.FoobarSpec@4a0e091e[name=<null>,aaaa=33,bbb=<null>,ccc=<null>,ddd=<null>,xxx=XY,eeee=eee,fff=fff,port=8200,

 [java] 08 May 2017 21:17:40,527  INFO FoobarTests.testSomething.95e0dc44-c51e-4b2f-a048-eba70eae6d94-60e04794-2fe8-42c6-b8a3-5ea180a20431 aaa.bbb.test.runner.ClientFactory:400 - Building a client for customer[123] using url-info[test.client.utils.Endpoint@78830d9a[envn=aaa,endPoint=<null>]].

我想循环遍历此文件colors.cfg

\033[0;31
\033[0;32
\033[0;33
\033[0;34
\033[0;35
\033[0;36
\033[0;37

并使用&#34;逗号和color.cfg&#34;中的一行替换日志文件中的每个逗号。每次都会读取不同的行。

我想使用它在日志文件中的每个逗号之后为文本提供不同的颜色。

所以我在新文件中得到这样的输出。

 [java] 08 May 2017 21:17:37,508  INFO FoobarTests.testSomething.95e0dc44-c51e-4b2f-a048-eba70eae6d94-60e04794-2fe8-42c6-b8a3-5ea180a20431 aaaa.aaa.test.:170 - Creating account: aaaa.aaaa.test.client.FoobarSpec@4a0e091e[name=<null>,\033[0;31aaaa=33,\033[0;37bbb=<null>,\033[0;36ccc=<null>,\033[0;35ddd=<null>,\033[0;34xxx=XY,\033[0;33eeee=eee,\033[0;32fff=fff,\033[0;31port=8200,

1 个答案:

答案 0 :(得分:1)

COLORS=()
while read -r line; do
  COLORS+=("${line}")
done </path/to/colors_file/colors_file.txt

IFS=','

while read -r logLine; do
  logLineParsed=''
  clrsIndx=0

  for linePart in ${logLine}; do
    if [ "${clrsIndx}" -eq "${#COLORS[@]}" ]; then
      clrsIndx=0
    fi

    logLineParsed+="${linePart},${COLORS[${clrsIndx}]}"
    ((clrsIndx++))
  done

  printf '%s\n\n' "${logLineParsed}" >> new_log_file.txt
done </path/to/log_file/log_file.txt

输出:

[java] 08 May 2017 21:17:37,\033[0;31508  INFO FoobarTests.testSomething.95e0dc44-c51e-4b2f-a048-eba70eae6d94-60e04794-2fe8-42c6-b8a3-5ea180a20431 aaaa.aaa.test.:170 - Creating account: aaaa.aaaa.test.client.FoobarSpec@4a0e091e[name=<null>,\033[0;32aaaa=33,\033[0;33bbb=<null>,\033[0;34ccc=<null>,\033[0;35ddd=<null>,\033[0;36xxx=XY,\033[0;31eeee=eee,\033[0;32fff=fff,\033[0;33port=8200,\033[0;34

[java] 08 May 2017 21:17:40,\033[0;31527  INFO FoobarTests.testSomething.95e0dc44-c51e-4b2f-a048-eba70eae6d94-60e04794-2fe8-42c6-b8a3-5ea180a20431 aaa.bbb.test.runner.ClientFactory:400 - Building a client for customer[123] using url-info[test.client.utils.Endpoint@78830d9a[envn=aaa,\033[0;32endPoint=<null>]].,\033[0;33