[Techtalk] real life kewl text-processing and regexps samples wanted

Telsa Gwynne hobbit at aloss.ukuu.org.uk
Sat Jan 10 13:59:16 EST 2004


On Fri, Jan 09, 2004 at 04:03:05PM -0800 or thereabouts, Carla Schroder wrote:
> Howdy gang,
> 
> I'm putting together a sort of 'text and file processing howto', 
> and I'm looking for real-life useful examples. 

I have saved tons of these over the years into a directory called
~/Tweaks. I know at least a couple of them came from Linuxchix in
the first place!

In my .bashrc are a couple which are all mine own.

For renaming ogg files which have been produced by 'grip'.
I want them numbered at the start so that command-line players
told to work on a directory play the album in the right order.
grip produces track names where the filename has no spaces in
it (which might be important here) but it doesn't number the 
tracks it produces. So,

  # This is a neat thing for numbering ogg tracks produced by grip.
  # Do:
  # num 01 first-track
  # num 02 second-track
  # etc.
  num() { mv $2 $1-$2 ; }

Similarly, if I am in a mindless "put that track on again and
again" mood -- well, three times, at least -- then,

  # This is even more fun.
  # For "thrice ogg123 sometrack"
  thrice () { $* ; $* ; $* ; }

..will play sometrack three times.

Another quick one which I like: get a bunch of tar and tar.gz
files into a directory, want to unpack them all, discover that
"tar xvf *" and "tar vfxz *" don't actually work as expected. 

What does work is

  for i in *.tar; do tar xvf $i; done
  for i in *.tar.gz; do tar xvfz $i; done

If these are source packages and you are thinking of installing
the program but you want to read the man page first, without
installing the whole app, just for that, here's how to read it.
Find the man page file: man pages usually have a name like
command.N where N is a number from 1 to 7 (or very occasionally
a lower-case 'n'). So for the pizza-maker program which someone
needs to write, it would be pizza.1 and you do

  groff -man -T ascii pizza.1

groff is the huge document-formatting package used by the man
command itself. -m is "include macro file" and for reasons I 
don't really follow but which you can find in "man groff_tmac"
you can get away with not including the initial "m" of a macro
name; so that -man is the same as -m man (ie, "use the macros 
for man pages"). -T is where to send the result (we want ascii). 

Finally, if the man page is more than about 30 lines, you
probably want a pager. So 

  groff -man -T ascii pizza.1 | less

or something similar. 

Reading "man groff" to check what this incantation does (I
neither remember nor care, usually, I just use it!) I spy
a typo, and must now sign off to file a really trivial bug 
which Debian have fixed and RH haven't :) 

Telsa



More information about the Techtalk mailing list