From: Brian Foster (blf at domain utvinternet.ie)
Date: Thu 11 Jul 2002 - 20:37:32 IST
| Date: Thu, 11 Jul 2002 17:10:41 +0200
| From: David Neary <dneary at domain wanadoo.fr>
| Subject: Re: [ILUG] Filtering a file.
since the original poster has since changed the spec, to one
which invites a different simpler solution, this discussion is
now just academic (but, IMHO, still interesting). of course,
if my reading of that original spec was correct rather than
your very plausible alternative, then Stephen Reilly had the
best solution.
| Brian Foster wrote:
| > | David Neary [ previously ] wrote:
| > | > sed '/^\/XXX\/ CE/{d;d}' filename
| > ↑
| > close, but not quite. │
| > yer missing a `;' here, ───┘
| > after the 2nd `d' before
| > the closing `}' [ ... ]
|
| Actually not, believe it or not. The semi-colon is a common, but
| non-standard extension to sed to allow multiple commands on the
| one line. And since the second d is the last command on the line,
| the semicolon isn't necessary as a separator.
I've cannot recall meeting a sed(1) which doesn't have `;',
albeit I also don't know what POSIX.2 (or .1b or whatever it
is nowadays) says. more to the point, I'm fairly certain I've
never met a `sed' where the originally posted version (i.e.,
sans the 2nd `;') would work. e.g., `GNU sed version 3.02':
$ # 1 1
$ # 123456789012345678
$ sed '/^\/XXX\/ CE/{d;d}' filename
sed: -e expression #1, char 18: Extra characters after command
$
$ sed '/^\/XXX\/ CE/{d;d;}' filename
[... no error, processes `filename', albeit incorrectly ...]
$
adding the `;' I claim is missing fixes the problem, as shown.
| the semicolon isn't necessary as a separator.
in what `sed' is it not necessary?? I'm puzzled as to which
`sed' does this .... ?
| >[ ... ] this somewhat obscure command does the trick:
| >
| > sed -n '\¬^/XXX/ CE¬{n;d;};p' filename
|
| Nope - doesn't quite do the job. If you have, say, the following
|
| /XXX/ CE
| /XXX/ CE
| stuff
|
| That will print
| stuff
| when (by my reading of the specs) it shouldn't print anything.
good point. however, I call this an ambiguity in the original
spec. whilst I no longer have the original posting readily
available, I don't recall the description discussing, nor the
sample data illustrating, this maybe-pathological case.
| I don't think -n to suppress printing could be called obscure,
| though...
true. but I had in mind the rarely(?)-used `n' command (and
the connection with `-n'), and (esp.) the rare `};' construct.
(also, AFAIK, the `\¬...¬' RE address notation is also rare,
but is, I _think_, in all(?) `sed's --- what does POSIX say??)
"rare" means "not commonly used (or even known)".
|[ ... ]
| > | and the trailing }
| > | needs to be on a line of it's own. Who knew!
| >
| > not quite. `}' is a command and hence needs to be separated
| > from the other commands, either by `;' or a newline.
|
| It's not getting passed to the shell - this is in sed that it's
| handled. And according to my man page, the closing } has to be on
| it's own line.
huh? who said anything about the shell? I _know_ it's not
being interpreted by the shell. in this context, "command"
means "sed command" (not "shell command (line)"), which is
the terminology used in the (GNU, 1998-05-07) sed(1) manual
page --- and does not mean the `sed' program itself, but
refers to the stuff `sed' interprets.
as such, `}' is very clearly called a "zero-address command".
but OTOH, `{' is called a "command which accepts an address
range", which is pedantically objectionable (because, unlike
all(?) other commands, it does not need to be separated from
the subsequent command). so season as to taste ....
I do, however, believe your (presumably non-GNU) sed(1) man
page says/implies the '}' has to be on its own line --- as I
recall, both the 7th Edition and some commercial *ix man pages
do seem to say just that. but, AFAIK, in all cases, adding
the (I claim missing) `;' works, despite the man page. ;-\
I am curious if you have/know a `sed' where adding that `;'
doesn't work, or, for that matter, if you have/know a `sed'
where your original neither-`;'-nor-newline posting works ...?
|[... `sed' program (script) using `sed's goto command ...]
| Maybe using a goto is cheating :) Can#t think of another way to
| do it right now, though.
nor can I. ;-\ strictly FYI, that script happens to very
close to a script I first wrote c.20 years ago(!) and still
have lying around somewhere, called `squeeze', which removes
all trailing whitespace and then --- this is where it is quite
similar --- collapses all consecutive empty lines into one.
thanks for an interesting discussion into some of the trivia
of `sed'.
cheers!
-blf-
| David Neary,
| Marseille, France
| E-Mail: bolsh at domain gimp.org
--
Innovative, very experienced, Unix and | Brian Foster Dublin, Ireland
Chorus (embedded RTOS) kernel internals | e-mail: blf at domain utvinternet.ie
expert looking for a new position ... | mobile: (+353 or 0)86 854 9268
For a résumé, contact me, or see my website http://www.blf.utvinternet.ie
Stop E$$o (ExxonMobile): «Whatever you do, don't buy Esso --- they
don't give a damn about global warming.» http://www.stopesso.com
Supported by Greenpeace, Friends of the Earth, and numerous others...
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:17:50 GMT