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

sed range

26 views
Skip to first unread message

ehabaz...@gmail.com

unread,
Jun 12, 2013, 6:15:44 PM6/12/13
to
I need to replace a string within a range of lines starting from finding a mark(another string).

change "\xe2\x80\x8f" with "?" of lines 1,12 after finding a string "BarCode"

sed -e "/BarCode/,12s/\xe2\x80\x8f/?/g" hold.out > soh.txt

Janis Papanagnou

unread,
Jun 12, 2013, 6:52:18 PM6/12/13
to
On 13.06.2013 00:15, ehabaz...@gmail.com wrote:
> I need to replace a string within a range of lines starting from finding a mark(another string).
>
> change "\xe2\x80\x8f" with "?" of lines 1,12 after finding a string "BarCode"

(Assuming by "after" you mean starting _the *line* after_ the match.)

>
> sed -e "/BarCode/,12s/\xe2\x80\x8f/?/g" hold.out > soh.txt
>

This is an awk newsgroup, so here's an awk solution...

awk 's&&s--&&sub(/\xe2\x80\x8f/,"?");/BarCode/{s=12}'

You said replacing "a string", but the sed contains a 'globally' flag.
In case you want it globally replaced per line use gsub() instead of sub().

Janis

Janis Papanagnou

unread,
Jun 13, 2013, 3:29:45 AM6/13/13
to
On 13.06.2013 00:52, Janis Papanagnou wrote:
> On 13.06.2013 00:15, ehabaz...@gmail.com wrote:
>> I need to replace a string within a range of lines starting from finding a mark(another string).
>>
>> change "\xe2\x80\x8f" with "?" of lines 1,12 after finding a string "BarCode"
>
> (Assuming by "after" you mean starting _the *line* after_ the match.)
>
>>
>> sed -e "/BarCode/,12s/\xe2\x80\x8f/?/g" hold.out > soh.txt
>>
>
> This is an awk newsgroup, so here's an awk solution...
>
> awk 's&&s--&&sub(/\xe2\x80\x8f/,"?");/BarCode/{s=12}'

To also print the unchanged lines in the 12-line range use...

awk 's&&s--{sub(/\xe2\x80\x8f/,"?")};1;/BarCode/{s=12}'
0 new messages