Re: Can I do it with sed?
From: Edgar Allen (eallen_at_allenhome.kc.rr.com)
Date: 08/29/03
- Next message: cyberdude: "Re: No color in Vim"
- Previous message: Tim Haynes: "Re: No color in Vim"
- In reply to: Bill Campbell: "Can I do it with sed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 29 Aug 2003 03:20:47 -0500
<html><input type crash></html>
begin Bill Campbell wrote:
> I'm trying to insert a fixed set of comment lines after the end of a
> subroutine declaration in a Fortran program. Subroutine declarations
> can either be on one line, vis.
>
> subroutine not_too_many_args(arg1,arg2)
> c Here's where I want the comment to go
>
> integer :: it(10)
> c ... but not here, or anywhere else in the file
>
> ... or they can span several lines, vis.
>
> subroutine why_so_many_args(arg1,arg2,
> & arg3,arg4,
> x arg5,arg6,
> 3 arg7)
> c Here's where I want the comment to go
>
> integer :: not_here(20)
> c ... but not here
> ,or_here(30)
> c ... or here, or there, I do not want it anywhere.
>
> What I have is a sed script that does not work:
>
> /subroutine/,/)/s/)/)\
> c Here is the comment\
> c and furthermore/
>
> The first pattern matches the first line, but the second pattern
> matches some later line in the first case (one-line subroutine
> declaration), while it works nicely in the second case (multi-line
> subroutine declaration). I guess I could use grep and split up the
> separate cases (I have several hundred subroutines to update), but I
> was hoping for a more elegant solution, ideally just using sed (what
> can I say, I like sed).
sed can have multiple actions more easily in a script file:
===============================================
/subroutine[^)]*)$/{
G
s/$/c Here's where I want the comment to go/
p
d
}
/subroutine[^)]*$/,/)$/{
/)$/{
G
s/$/c Here's where I want the comment to go/
}
}
===============================================
invoked with:
sed -f comment.sed source.f >commented_source.f
The first handles a single line subroutine declaration and
the second is for multi-line.
You want to look for 'subroutine' and keep going until you
have a line ending in ')'
Once 'subroutine' is matched any line ending in ')' will append
the comment line. If the 'subroutine' and the ')$' happen to
be on the same line then sed ignores that you want it to terminate,
append a comment and begin looking for the next 'subroutine'
and instead would match the assignment line to add the comment.
So I have put a second case to handle single line declarations
before the multi-line one.
The 'p' and 'd' combo first prints the pattern space
and then the 'd' aborts the interpretation and restarts from the top
with the 'subroutine' state unset for the next line.
I hope that is understandable but it is late here and I am in need
of sleep. Good night.
-- Linux is resistant to viruses because it was modeled on Unix which has security honed by conniving, unscrupulous college students over generations.
- Next message: cyberdude: "Re: No color in Vim"
- Previous message: Tim Haynes: "Re: No color in Vim"
- In reply to: Bill Campbell: "Can I do it with sed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|