Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED Sctipt

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
Hi,

I am writing a sed script to perform a task.

However, one of the lines I want to modify has 2 matches in the file.

How can I specify I only want to modify the second occurance?
 
Can you please post your current sed script ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes;

sed '/^RolloverSize=/s/0xa;/0x64;/' registry> tmp && mv tmp registry


The registy file has two enties;

RolloverSize= 0x64; REG_DWORD
TraceConfig= ; REG_SZ
RolloverSize= 0x64; REG_DWORD

I want to target the second instance only.
 
You may try something like this:
awk '
{b=$0}
/^RolloverSize/{++i;if(i==2)sub(/0xa;/,"0x64",b}
{print b}
' registry> tmp && mv tmp registry

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Can I not just use a line parameter within SED?

Like the g for global parameter?
 
HI PH.

This file does not work for me...

Any more suggestions?
 
Try ...

awk '/^RolloverSize=/&&c++{sub("0xa;","0x64;")};1' registry> tmp && mv tmp registry
 
This file does not work for me
Can you please be more descriptive ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PH,

THis is the error;

$ ./test.sh

awk: cmd. line:3: /^RolloverSize/{++i;if(i==2)sub(/0xa;/,"0x64",b}
awk: cmd. line:3: ^ syntax error

Stu
 
OK, you may try this:
awk '
{b=$0}
/^RolloverSize/{++i;if(i==2)sub("0xa;","0x64;",b)}
{print b}
' registry> tmp && mv tmp registry

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top