For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > replace only the first occurrence of each paragraph









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author replace only the first occurrence of each paragraph
ibmichuco@hotmail.com

2005-11-25, 6:55 pm

Hi,

I would like to replace only the first occurrence of a string of each
paragraph, and this is what I
patched up from earlier posts of this group: (Couldn't find much info
about this in Dougherty and Robbins' book)

awk -v RS="" 'NR==1,/old/{sub(/old/,"new")}1;{print " "}' filename

where my paragraphs are separated by a blank line. This only work for
the first para. Any suggestion?

Thanks in advance,

Michuco

Janis Papanagnou

2005-11-25, 6:55 pm

ibmichuco@hotmail.com wrote:
> Hi,
>
> I would like to replace only the first occurrence of a string of each
> paragraph, and this is what I
> patched up from earlier posts of this group: (Couldn't find much info
> about this in Dougherty and Robbins' book)
>
> awk -v RS="" 'NR==1,/old/{sub(/old/,"new")}1;{print " "}' filename
>
> where my paragraphs are separated by a blank line. This only work for
> the first para. Any suggestion?


Quite correct but the NR==1 restricts your processing to the first record.

BEGIN { RS=""; FS="\n" }
{ sub(/old/,"new") } 1
{ print "" }


Janis


> Thanks in advance,
>
> Michuco
>

ibmichuco@hotmail.com

2005-11-29, 6:56 pm

Janis,

Many thanks. Works perfectly.

Michuco

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com