LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Filtering a file.

[ILUG] Filtering a file.

David Neary dneary at wanadoo.fr
Thu Jul 11 16:06:23 IST 2002


Brian Foster wrote:
>   | David Neary [ previously ] wrote:
>   | > OK - this may not work, but the idea is sound enough.
>   | > 
>   | > sed '/^\/XXX\/ CE/{d;d}' filename
>                             ???
>  close, but not quite.      ???
>  yer missing a `;' here, ????????????
>  after the 2nd `d' before
>  the closing `}', i.e.:

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. The curly bracket
does need to go on a line of it's own, though.

>  they are not correct because `d' starts the next cycle,
>  i.e., the next line is read and the program starts from the
>  beginning.  hence, the 2nd `d' is never executed, and thus
>  the following line is printed.  but it should have been
>  "removed" (not printed) ....   ;-(

Wow, you're right. d discards the pattern space and loops... you
would have to use the n;n solution.

>  instead, 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.

I don't think -n to suppress printing could be called obscure,
though...

>   | Sed commands should be on separate lines,
> 
>  IMHO, it's a matter of taste/style.

As I said, surprisingly not. If you're sticking to POSIX sed,
then the behaviour of ; is not defined, and the commands have to
be on different lines. In any case, the trailing curly bracket 

>   |                                           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.

>   | sed -n '/^\/XXX\/ CE/{
>   | n
>   | n
>   | }
>   | /^\/XXX\/ CE/! p' filename
> 
>  close but not quite.  it will fail on the input:
> 
>     /XXX/ CE one, 1st line
>                   2nd line
>     /XXX/ CE two, 3rd line
>                   4th line

Ack! Yes, I was misunderstanding how the flow control worked. The
second n, changed to d, repairs it, and makes it almost
equivalent to yours. It also had the flaw that your version had.

>  thanks for the corrections.  I hope my comments above are also
>  useful and not too misleading.

Well, we differ on some stuff :) Actually, I think this might be
the best try yet...

sed -n '
:start
/pattern/{
n
/pattern/b start
d
}
p' testfile

Maybe using a goto is cheating :) Can#t think of another way to
do it right now, though.

Dave.

-- 
       David Neary,
    Marseille, France
  E-Mail: bolsh at gimp.org




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell