sed multiline pygmentize - sed

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:
Sed multiline replacement question
Using or in multiline sed replacement
sed or awk multiline replace
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)

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>/'

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')

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)

Related

Break lines in powershell array and then convert it to html?

I swear I've been searching a lot and still haven't found any solution to this thing.
What I do is basically converting arrays to HTML but I can't find any way to break the words of the array in new lines.
For example:
$Member.ExpireOn=((Invoke-SqliteQuery -SQLiteConnection $C -Query "SELECT expireon FROM exceptions_test WHERE (identity='$User')").ExpireOn -join "`r`n")
$Member.ExpireOn is basically this:
31/01/2019 00:00:00 31/03/2019 00:00:00 31/03/2019 00:00:00
so even if I join it with -join "rn" or with <br> I can't find any way to have a line for each element in the array.
I need to do it because after that I print the whole array in a HTML file and this is what I get:
instead of having a <br> between those words.
Hopefully somebody can help me :)
even if I join it with -join "`r`n"
Whitespace in HTML is insignificant, so you cannot force line breaks this way; they will show in the HTML markup (source code), but not when rendered.
or with <br>
The problem is that ConvertTo-Html - which I assume you're using - escapes any HTML markup in the input objects' property values (it assumes you want to use the values verbatim), so you cannot pass <br> through in order to make a single table cell use multiple line - you'll see literal <br> strings in the table, because ConvertTo-Html has escaped them as <br>.
A quick and dirty workaround would be to manually convert the escaped <br> elements back to their HTML form:
# Sample input objects
$o = [pscustomobject] #{
Foo = 'Cert A'
# Join the array elements with <br>
ExpireOn = (Get-Date), (Get-date).AddDays(1) -join '<br>'
}, [pscustomobject] #{
Foo = 'Cert B'
ExpireOn = (Get-Date).AddDays(2), (Get-date).AddDays(3) -join '<br>'
}
# Convert *escaped* <br> elements back to literal '<br>'
# NOTE: This will replace *all* instances of '<br>' in the
# document text, wherever it may occur.
($o | ConvertTo-Html) -replace '<br>', '<br>'
This yields the following, showing that the <br> was effectively passed through, which should make the input date values render on individual lines:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/></colgroup>
<tr><th>Foo</th><th>ExpireOn</th></tr>
<!-- Note the <br> elements -->
<tr><td>Cert A</td><td>2/5/2020 12:51:45 PM<br>2/6/2020 12:51:45 PM</td></tr>
<tr><td>Cert B</td><td>2/7/2020 12:51:45 PM<br>2/8/2020 12:51:45 PM</td></tr>
</table>
</body></html>
In Chrome on my Mac, this renders as follows:
If you need a more robust solution, you'll have to use an HTML parser - see this answer.

sed replace string except the matched string

Problem statement:
change every user agent that does not match A2PC or GENCOM with the user agent PROHIBITED and keep GENCOM and A2PC unchanged
Expression:
echo \"GENCOM\" | sed -r -e 's/(^((?!A2PC)(?!GENCOM).)*$)/PROHIBITED/g'
error:
sed: -e expression #1, char 41: Invalid preceding regular expression
I removed -r then error not thrown but its not working
echo \"GENDFGGH\" | sed -e 's/(^((?!A2PC)(?!GENCOM).)*$)/PROHIBITED/g'
"GENDFGGH"
Please help me for this solution
First look for your pattern and then do the sub:
# echo \"GENCsOM\" | sed -e '/^"\(GENCOM\|A2PC\)"$/! s/^.*$/PROHIBITED/'
PROHIBITED
# echo \"GENCOM\" | sed -e '/^"\(GENCOM\|A2PC\)"$/! s/^.*$/PROHIBITED/'
"GENCOM"
sed '/A2PC/ !{
/GENCOM/ ! {
s/$/PROHIBITED/
}
}' YourFile
double exclusion than a change, posix compliant

SED renaming with unknown amount of characters before a "

I have a file1 that has some PHP code in it. I need to find the following: action="blahblah" and replace it with action="error.php". Problem is, I don't know how many characters are between the quotes in the original.
Here's what I have that doesn't work:
sed 's:action="^[^"]*":action="error.php":' <file1> file2
How can I do this?
Why have you got the ^ start-of-line marker before the character class? Try it with:
sed 's:action="[^"]*":action="error.php":' <file1 > file2
Here's a transcript showing your version alongside that correction:
pax$ echo 'blah action="something" blah' | sed '
...$ s:action="^[^"]*":action="error.php":'
blah action="something" blah
pax$ echo 'blah action="something" blah' | sed '
...$ s:action="[^"]*":action="error.php":'
blah action="error.php" blah

How to extract jmx statistics from jmx interface only from command line?

I want to extract this data http://code.google.com/p/memcached-session-manager/wiki/JMXStatistics via jmx but using only command line.
This is because is the only way to enter to the server I got.
Any pointer would help
thanks in advance
I did up this quick-n-dirty solution after banging my head against the JMX protocol's inability to tunnel out over ssh.
(1) found some jmxterm thing, (2) stuff some simple command into it (runs an MBean operation) (after examining what JConsole did when attached to local tomcat..) and (3) format it into something readable.
#!/bin/bash
# wget http://downloads.sourceforge.net/cyclops-group/jmxterm-1.0-alpha-4-uber.jar
JARFILE="jmxterm-1.0-alpha-4-uber.jar"
JMXHOST=127.0.0.1
JMXPORT=9003
function die() { echo $1 ; exit 1 ; }
function checkexe() { [ -x `which $1` ] || die "cant find executable program $1" ; }
PROGS="java grep cat sed tr xsltproc xmllint"
for P in $PROGS ; do checkexe $P ; done
[ -f $JARFILE ] || die "can't see jar file $JARFILE"
## jmxterm commands to get thread stack dump
cat >/tmp/myscript.jmx<<EOF
domain java.lang
bean java.lang:type=Threading
run dumpAllThreads 1 1
quit
EOF
## get the stack traces
java -jar $JARFILE -l $JMXHOST:$JMXPORT -i /tmp/myscript.jmx > /tmp/allthreads.txt
grep "threadId" /tmp/allthreads.txt || die "stack trace get seemed to fail ?!"
## turn them into xml
cat /tmp/allthreads.txt | \
sed \
-e "s|\[ |<array>|g" -e "s| \]|</array>|g" \
-e "s|{|<obj>|g" -e "s|}|</obj>|g" \
-e "s|\([^ <>]*\) = \([^ <>]*\);|<\1>\2</\1>|g" \
-e "s|\([^ ]*\) = \([^;]*\) *;|<\1>\2</\1>|g" | \
tr '\r\n\t' ' ' | tr -s ' ' | \
sed \
-e "s|\([^ =]*\) = \([^;=]*\);|<\1>\2</\1>|g" \
-e "s|\([^ ]*\) = \([^;]*\);|<\1>\2</\1>|g"\
-e "s| *, *||g" > /tmp/allthreads.xml
## xsl to convert xml-ified stack traces to simpler format
cat >/tmp/jmxterm_threads.xslt<<EOF
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<jmx>
<xsl:for-each select="/array/obj">
<xsl:sort select="threadId" data-type="number"/>
<xsl:apply-templates select="." mode="thread"/>
</xsl:for-each>
</jmx>
</xsl:template>
<xsl:template match="obj" mode="thread">
<thread id="{threadId/text()}">
<xsl:copy-of select="threadName"/>
<xsl:copy-of select="threadState"/>
<xsl:copy-of select="suspended"/>
<xsl:copy-of select="inNative"/>
<xsl:copy-of select="waitedCount"/>
<xsl:copy-of select="blockedCount"/>
<xsl:apply-templates select="stackTrace"/>
</thread>
</xsl:template>
<xsl:template match="stackTrace">
<stackTrace>
<xsl:for-each select="./array/obj">
<stackfn class="{className}" method="{methodName}" file="{fileName}" line="{lineNumber}"/>
</xsl:for-each>
</stackTrace>
</xsl:template>
</xsl:stylesheet>
EOF
## simplify xml
xsltproc /tmp/jmxterm_threads.xslt /tmp/allthreads.xml | xmllint --format - > /tmp/allthreads_simple.xml
cat /tmp/allthreads_simple.xml
For CLI access I recommend jmx4perl and the readline based JMX shell j4psh. They require a Jolokia agent on the the server side, though. These agents are available in several flavours (war, osgi, mule, jdk6). Since memcached-session-manager runs within a Tomcat, you probably should use the WAR agent and simply drop it into the webapps/ directory.

sed - matching escape char, backslash+double quote

I'm trying to find replace the following
<a href=\"test\">
with
<a href="test">
using sed.
I understand that both the \ and the " need to be escaped, so I do the following:
sed -i "s|a href=\\\"test\\\"|a href=\"test\"|g"
but this doesn't seem to work. Any idea what I'm doing wrong?
Just use single quotes instead of doubles.
$ echo '<a href=\"test\">' | sed "s|a href=\\\"test\\\"|a href=\"test\"|g"
<a href=\"test\">
$ echo '<a href=\"test\">' | sed 's|a href=\\\"test\\\"|a href=\"test\"|g'
<a href="test">
Ori, if you can't use single quotes, just add even more escapes :-)
$ echo '<a href=\"test\">' | sed "s|a href=\\\\\"test\\\\\"|a href=\"test\"|g"
<a href="test">