Highlight the plain text in XML (or HTML, SGML, etc)

xmlpager() { xmlindent "$@" | awk '{gsub(">",">'`tput setf 4`'"); gsub("<","'`tput sgr0`'<"); print;} END {print "'`tput sgr0`'"}' | less -r; }
Don't want to open up an editor just to view a bunch of XML files in an easy to read format? Now you can do it from the comfort of your own command line! :-) This creates a new function, xmlpager, which shows an XML file in its entirety, but with the actual content (non-tag text) highlighted. It does this by setting the foreground to color #4 (red) after every tag and resets it before the next tag. (Hint: try `tput bold` as an alternative). I use 'xmlindent' to neatly reflow and indent the text, but, of course, that's optional. If you don't have xmlindent, just replace it with 'cat'. Additionally, this example shows piping into the optional 'less' pager; note the -r option which allows raw escape codes to be passed to the terminal.
Sample Output
$ xmlpager LuminoCity/assets/conversations/eng/*/*.xml
<?xml version="1.0" encoding="UTF-8"?><convoscene>
        <conversation name="PumpSailorIntro" flow="loop" loopPoint="3">
                <line box="Lumi">Do you mind if I ask why there's a boat up here?</line><line box="PumpSailor">Well now, 'why' would suggest this was supposed to happen.</line><line box="PumpSailor">This wasn't in the Capt'n's plans, way back when.</line><line box="PumpSailor">There we were.
                    Navigating the river out to the west ocean.
                <line box="PumpSailor">Night time. Couldn't see the hand infront of my face. 
                    Or an almighty wheel, come to that.
                <line box="PumpSailor">Next thing we know, almighty crunch!</line><line box="PumpSailor">Boat stops rocking.
                    Starts floating.
                <line box="PumpSailor">Then there's this mighty grinding sound. 
                    The wheel, grinding its gears.
                <line box="PumpSailor">Boat stops moving.
                    Right here.
                <line box="Lumi">You're stuck?</line><line box="PumpSailor">Aye. 
                    Ran a-ground, we did.
                <line box="PumpSailor">Ran a-sky.</line>

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: