Archive for the ‘Code’ Category

Sed One-Liner to Mass-Rename

Tuesday, July 20th, 2010

Linux’s perl package has a command called “rename” which understands regular expressions.

Windows lacks that, of course. I’ll recreate it using sed, in a way that’s consistent across platforms…

It won’t spring to mind when you need it (unless you’re really familiar with sed), but the exercise is a decent excuse to demonstrate the oft-neglected hold buffer.
 
(more…)

RancidMeat: XMLStarlet

Monday, April 26th, 2010

Occasionally I have to parse xml, which is often not grep-friendly. Luckily, there’s XMLStarlet (the actual command’s named ‘xml’) to make it relatively easy, if longwinded for a prompt. It’s in Linux repositories, and there’s a Windows version.

For this post, I’ll be referring to an example file that looks like this.

<?xml version="1.0"?>
<Cast>
<Character gender='M'>
  <Name>Fry</Name>
  <Job>Delivery Boy</Job>
  <Species>Human</Species>
  <From>
    <Location>Earth</Location>
    <Location>New York</Location>
    <Location>Brooklyn</Location>
  </From>
</Character>

</Cast>

 
 
1) Loop through all characters, printing the value of their name tag and a newline.

xml sel -t -m "/Cast/Character" \
-v "Name" \
-n futurama_cast.xml

# Zapp
# Leela
# Amy
# Zoidberg
# Fry
# Bender
# Professor
# Kif

 
(more…)

Batch For Loop Over a List

Thursday, April 15th, 2010

Say you’ve got a Windows command to run over and over with a different arg each time. Wouldn’t it be nice if you could make a word list and loop over it the way bash does?
 
(more…)

Echoing Multiple Lines into a Pipe

Monday, March 29th, 2010

If you’ve been batch scripting on Windows for a while, you probably know the way to echo a blank line: a period with no space in front of it.

echo.

And the way to chain commands together:

  • A || B runs B if A failed
  • A && B runs B if A succeeded
  • A & B runs B regardless

It may not be obvious, but you can echo multiple lines into a pipe without resorting to a temp file.

(echo def& echo abc) | sort

You will have to butt the ampersand against the end of each echo, or you’ll be sending spaces at the end of each line.

If you make a lot of lines, mashing them together gets hard to read. The solution to that obscure situation is below.
 
Edit (2010-04-25): Argh! Ampersand butting works for redirecting to a file, and FOR loops (as you’ll see in a later post), but plain ol’ pipes still get trailing spaces!
Example: Pipe into sort, get spaces. Dump to tmp.txt, no spaces.
My “Right Way”, below the fold, somehow only got a space on the final line.
 
(more…)

Managing Path Dependencies

Thursday, January 14th, 2010

So you’ve got some commandline tools but they each depend on a bunch of dlls? You could dump the whole mess together in one dir, but that’d be awful. Adding a dozen tool-specific dirs to your PATH isn’t pretty either. The answer: symlinks!
 
What’s that? Windows doesn’t have symlinks? How about shortcuts? No luck there. Well there are still batch files…
 
(more…)

Messing with the Clipboard

Sunday, September 27th, 2009

So you want to dabble in commandline wizardry, but you don’t want to get into that hard bash scripting stuff just yet? With these, you can live in the GUI and use a terminal to mangle your clipboard.
 
For the casual piping exercise we’ll sort this list:

Hermes
Zapp
Leela
Amy
Zoidberg
Fry
Bender
Professor
Kif

 
I’ve got instructions for each platform below the fold.
And for advanced folks, there’s a way to run a script without saving a text file.
 
(more…)