[Sussex] Updated Grep, Sed and RegExp links from August moot
Dominic Humphries
linux at oneandoneis2.org
Fri Sep 16 08:09:24 UTC 2011
On Fri, 2011-09-16 at 03:14 +0100, Steve Dobson wrote:
> sed is an extremely powerful editor. I use it for changing typos and
> stuff in boilerplate comments in files for example.
Oh, certainly - I used it extensively when I was working through
LinuxFromScratch. It's well worth knowing.
> > You could replace with the below perl script, which you would run as, e.g.:
> > ./de-page-number filename.txt
> > And it would create filename.txt.out with the de-paginated output.
> >
> > I'll grant you it would be more typing, but it's a *lot* more
> > readable & re-usable :)
>
> Your perl script maybe more readable but I would argue that it isn't
> more re-useable.
Ah. Yes, my wording wasn't terribly clear, sorry about that: I wasn't
referring to the script, I was referring to Perl in general - it's
easier to understand & modify a well-commented script file to do
something slightly different than it is to search back through your
commandline history to find one specific sed regex amongst dozens of
others.
> You have to use it on a file, and the modified text is
> written to another file in the same directory. What if the file you
> want to de page number is on a CDROM?
Prepend a source directory to the output filename before you run it :o)
Or modify the script to accept an output file as an argument as well as
the input file. Or modify the script so it'll default to outputting a
'dummy run' of the file, showing the changes it would make; and then
pass it an option to modify the file in-place.
The Perl script I attached was purely a quick & simple example of a
script that could be used in place of the specified sed command, not a
full-featured "This is how I would do it" example - I wrote it whilst I
was supposed to be working after all :o)
> First of all we can turn the sed expression into a command by creating
> the following file:
True, if you're saving sed commands into files rather than just running
them directly in the shell, then they become a lot easier to organise &
re-use.
But when you start getting into things like multi-line edits, which
judging by one of Fay's other examples is something that she does; I
maintain that even if you can write a regex for it (which you usually
can, regexes being awesome :o) it tends to be so horrendously long and
complicated that it's easier to write a new one from scratch each time
than try to find, decipher & modify existing ones.
As an example, here's a Vim regex I use frequently. How long does it
take you to work out exactly what I use it for?
:'a,'bs/^\s\+\d\d\s\+\(\S\+\).*pic 9.*/move "\U\1\L" to sort-name\rmove \L\1 to pretty-integer-in\rperform send-integer-item\r/i
It's often possible to use a one-line regex in place of a dozen or two
lines of logic; but it's far easier to understand & modify a dozen lines
of well-commented code than it is to do the same for a complex regex.
> But at the end of the day the really important thing is to learn
> regexps,
Agreed, regexes are massively useful.
> although one does have to be careful because there are some
> differences from command to command.
Also agreed - the way vim treats brackets as normal characters that you
have to escape to make them regexy, rather than the more normal
vice-versa way around, tends to confuse the hell out of my newer
co-workers :o)
Dominic
More information about the Sussex
mailing list