sed multiline pygmentize

2019-09-11 05:19发布

I would like to take the html piece and pass it to pygmentize to colorize it accordingly. I'm wondering how I could use sed or some other cli tool to get that accomplished.

I tried a bunch of sed one-liners and tried to use the following SO questions:

I have the following log:

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]

UPDATE I'm adding this to a longer command:

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条回答
萌系小妹纸
2楼-- · 2019-09-11 05:30

I used this as part of a longer command:

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楼-- · 2019-09-11 05:33

TXR:

For illustration purposes, we replace pygmentize with a command that replaces every letter in the HTML with X.

@;; 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)

Test run:

$ 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')
查看更多
疯言疯语
4楼-- · 2019-09-11 05:42

cat log | awk '/<html>/,/<\/html>/'

Should do it.

To remove the "crap" before the first html tag, get sed to put the html tag on it's own line.

cat log | sed 's/<html>/\n<html>/' | awk '/<html>/,/<\/html>/'

查看更多
登录 后发表回答