sed multiline pygmentize

时间:2012-03-26 22:42:59

标签: sed command-line-interface

我想把html片段传递给pygmentize来相应地着色它。我想知道如何使用sed或其他一些cli工具来实现这一目标。

我尝试了一堆sed one-liners并尝试使用以下SO问题:

我有以下日志:

2012-03-26 18:04:27,385 9372 [main] ERROR web.commons.exception.ServiceInvocationException  - 
Response from server cannot be decoded to JSON, responsePayload = <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing jetty-url. Reason:
<pre>    Not Found</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

</body>
</html>

org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@369133f6; line: 1, column: 2]

更新我将此添加到更长的命令中:

mvn -U test | (while read line; do echo ${line} | sed -e "s/.*ERROR.*/`echo -e '\e[91m&\e[0m'`/g" -e "s/.*\(WARN|INFO\).*/`echo -e '\e[93m&\e[0m'`/g"; done)

3 个答案:

答案 0 :(得分:2)

  

cat log | awk'/&lt; html&gt; /,/&lt; \ / html&gt; /'

应该这样做。

要删除第一个html标记之前的“废话”,请使用sed将html标记放在它自己的行上。

  

cat log | sed's /&lt; html&gt; / \ n&lt; html&gt; /'| awk'/&lt; html&gt; /,/&lt; \ / html&gt; /'

答案 1 :(得分:0)

TXR:

为了便于说明,我们将pygmentize替换为用X替换HTML中每个字母的命令。

@;; replace with .e.g. pygmentize
@(bind filter "tr [A-Za-z] X")
@date @time @pid [@function] @error_1
@error_2 <html>
@(collect)
@stuff
@(last)
</html>
@(end)

@error_3
@(output)
@date @time @pid [@function] @error_1
@error_2
@(end)
@(output `!@filter`)
<html>
@{stuff "\n"}
</html>
@(end)
@(output)
@error_3
@(end)

试运行:

$ txr  log.txr  log.txt
2012-03-26 18:04:27,385 9372 [main] ERROR web.commons.exception.ServiceInvocationException  -
Response from server cannot be decoded to JSON, responsePayload =
<XXXX>
<XXXX>
<XXXX XXXX-XXXXX="XXXXXXX-XXXX" XXXXXXX="XXXX/XXXX; XXXXXXX=XXX-8859-1"/>
<XXXXX>XXXXX 404 XXX XXXXX</XXXXX>
</XXXX>
<XXXX><X2>XXXX XXXXX 404</X2>
<X>XXXXXXX XXXXXXXXX XXXXX-XXX. XXXXXX:
<XXX>    XXX XXXXX</XXX></X><XX /><X><XXXXX>XXXXXXX XX XXXXX://</XXXXX></X><XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>
<XX/>

</XXXX>
</XXXX>
org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

答案 2 :(得分:0)

我将此作为较长命令的一部分使用:

mvn -U test | (while read line; do echo ${line} | sed -e "s/.*ERROR.*/`echo -e '\e[91m&\e[0m'`/g" -e "s/.*\(WARN|INFO\).*/`echo -e '\e[93m&\e[0m'`/g"; done)
相关问题