Re: how long does it take
From: Laurent Vogel (lvl_at_club-internet.fr)
Date: 07/29/03
- Next message: Ben: "Re: trap and exit status"
- Previous message: Michael Wang: "Re: trap and exit status"
- In reply to: Arianne Lefebvre: "how long does it take"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Jul 2003 19:58:41 +0200
Arianne wrote:
> How long does it take [sed adepts] to write what looks
> like a complex sed script i.e. this one recently submitted here:
>
> sed '/[^ ]/{s/ *\(.\).*/\1/;H;d;};s/.*//;x;s/\n/ /g;s/ //' input > output
>
> I only use sed and other tools like it once every couple of months, and I
> always have to go back and look things up to remind myself of syntax and
> special character quoting etc.
Personnally I posess the syntax, and know what is easy to do and what
is not, so I can easily "visualize" what steps are easiest to take.
OTOH there are parts of SED which I don't master at all (intervals, for
instance), so I don't use these at all.
> I am just curious, when you're working with
> these tools everyday and you're smart and you have a good memory, can you
> think of how to do this and write it in like 20 secs, or do you have to
> scratch your head for a minute and make a quick trial version, or is
> it still more than 5 mins labour for you?
well, more like 5 minutes. I've got no problem of syntax. I first
paste a sample text in a file, then figure roughtly a logic in my head.
In this case, the logic is very simple:
if there is a non-space char on the line | /[^ ]/{
then keep it and remove everything else | s/ *\(.\).*/\1/
accumulate it to the hold buffer | H
and stop processing this line | d
| }
now, I must print the line somehow, so there will be a kind of:
get back the line from the hold buffer | g
remove all \n | s/\n//g
and that's the first draft. All commands used there are very simple,
atomic commands (no complex regexps) so they are written very easily.
At this point I test, figure out that I've forgotten to clear the hold
buffer => replace 'g' by 's/.*//;x'
and also that an additional space should be deleted: 's/ //'
Of course if I really want to create a robust script, then I must
think of all which can go wrong (for instance here, the last line
is dropped if there is no empty line at the end), but for usenet
posts I think it suffices to illustrate how the real script can
work.
Laurent
- Next message: Ben: "Re: trap and exit status"
- Previous message: Michael Wang: "Re: trap and exit status"
- In reply to: Arianne Lefebvre: "how long does it take"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|