You are reading a single comment by @Greenbank and its replies. Click here to read the full conversation.
  • One line bash command. Like.

    Thanks for this.

    My regex is terrible - what dies this bit cover: /^ */

    Do you have one that automatically formats / indents xml files? tcx / gpx files on one line give me a headache.

  • 's/^ *//' replaces any spaces at the beginning of the line with nothing, essentially stripping off leading whitespace.

    ^ matches the beginning of the line
    the name space character matches at least one space
    space then kleene star (*) matches any number (including 0) spaces
    replace it with nothing.

    's/^ +//' would be cleaner (match beginning of line and then 1 or more spaces) but I've been bitten by lack of + support in the dim past and so I naturally avoid it.

    As for indentation, something like xmllint will do a better job, but for hackiness you could also just do something along the lines of:-

    cat a.gpx | tr -d '\n' | sed -e 's/> *</>\n</g' | perl -e '$in=0; while( <> ) { if( /<[^\/].*<\// ) { print " "x$i.$_; } elsif( /<[^\/]/ ) { print " "x$i.$_; $i++; } elsif( /<\// ) { $i--; print " "x$i.$_; } }'
    

    The sed separates tags onto a line by themselves and the perl does some rudimentary indentation.

    Expect the above to blow up (especially for strings that contain embedded > or < characters). It's 5 minutes' work whilst distracted on a conference call.

  • I think I used awk previously to chuck everything onto new lines.

    That was a long time ago, and my scripting now involved direct copypasta, and shouting if it doesn't do what I want.

About

Avatar for Greenbank @Greenbank started