Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing Some Text

1 view
Skip to first unread message

Scot Johnson

unread,
Dec 29, 2009, 1:36:59 PM12/29/09
to
Hi,

I'm new to awk. I have a ConTeXt file where I'm using
"\startconcept{foo:}" and "\startconcept{foo}" interchangeably. I'd
like to make all of them look like the former with the ":". I
developed the following which prints only the lines without the colon:
"gawk /startconcept.*[^:]\}/ path/to/file", but I can't figure out how
to replace the right "}" with ":}". I had hoped to use the "sub"
function but I can't get it to work.

Any suggestions?

Thanks.

Scot

Janis Papanagnou

unread,
Dec 29, 2009, 1:47:53 PM12/29/09
to
Scot Johnson wrote:
> Hi,
>
> I'm new to awk. I have a ConTeXt file where I'm using
> "\startconcept{foo:}" and "\startconcept{foo}" interchangeably. I'd
> like to make all of them look like the former with the ":". I
> developed the following which prints only the lines without the colon:
> "gawk /startconcept.*[^:]\}/ path/to/file", but I can't figure out how

Why is your awk program unquoted?

Is there only one matching expression per line?

> to replace the right "}" with ":}". I had hoped to use the "sub"
> function but I can't get it to work.
>
> Any suggestions?

Try

gawk '/startconcept.*[^:]\}/ { sub(/}/,":}") }
{ print $0 }' /path/to/file


Janis

>
> Thanks.
>
> Scot

Scot Johnson

unread,
Dec 29, 2009, 2:08:25 PM12/29/09
to
Janis Papanagnou wrote:
> Scot Johnson wrote:
>> Hi,
>>
>> I'm new to awk. I have a ConTeXt file where I'm using
>> "\startconcept{foo:}" and "\startconcept{foo}" interchangeably. I'd
>> like to make all of them look like the former with the ":". I
>> developed the following which prints only the lines without the colon:
>> "gawk /startconcept.*[^:]\}/ path/to/file", but I can't figure out how
>
> Why is your awk program unquoted?
Simply a mistake.

>
> Is there only one matching expression per line?
Yes, only one matching expression per line

>
>> to replace the right "}" with ":}". I had hoped to use the "sub"
>> function but I can't get it to work.
>>
>> Any suggestions?
>
> Try
>
> gawk '/startconcept.*[^:]\}/ { sub(/}/,":}") }
> { print $0 }' /path/to/file
Works perfectly. I was very close, but sadly couldn't get it right.
Thanks for your help!

Scot

Ed Morton

unread,
Dec 29, 2009, 5:05:44 PM12/29/09
to

That would have an undesired effect on lines that have a "}" before the matching
pattern, e.g.

...} startconcept{foo}

I'd just use:

awk '{sub(/\\startconcept{foo}/,"\\startconcept{foo:}")}1' /path/to/file

Regards,

Ed.


Scot Johnson

unread,
Dec 29, 2009, 5:41:32 PM12/29/09
to
Hi Ed,
Janis Papanagnou's suggestion worked for me, since \startconcept is
always anchored to the start of the line. There are no cases of

...} startconcept{foo}

in the document.

Thanks!

Scot

0 new messages