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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED - Need to change string but maintain original case

Status
Not open for further replies.

smitp11

Programmer
Jan 15, 2004
4
0
0
GB
Hi,

I need to edit a file using SED and replace a string but maintain the case of the original string.

ie Change:

HRP1 to HRS1
hrp1 to hrs1
HrP1 to HrS1

I can do this with multiple edit commands but wondered if there was a way of doing it in one edit command ?
 
Try it:

/[Hh|Rr].1/{
s/P1/S1/
s/p1/s1/
}

tikual
 
sorry for my typo:

/[Hh][Rr].1/{
s/P1/S1/
s/p1/s1/
}

tikual
 
I could not get that command to work:

sed /[Hh|Rr].1/{s/P1/S1/s/p1/s1/} D:\pat.txt > D:\pat_mod.txt

returns:

sed: newline or end of file found in pattern
The name specified is not recognized as an
internal or external command, operable program or batch file.

 
Try again:
sed '/[Hh][Rr].1/{s/P1/S1/g;s/p1/s1/g;}' D:\pat.txt > D:\pat_mod.txt


tikual
 
tikual,

That's work a reat. Many Thanks. Second question, how would you do the same thing for:

trn2452 to trn2453
TRN2452 to TRN2453 etc

Regards.
 
Try with this sed script:
Code:
'/[tT][rR][nN]2452/s/2452/2453/'

Hope This Help
PH.
 
Try it:

sed '/TRN[0-9]*2/{s/\(TRN[0-9]*\)2/\13/g;};/trn[0-9]*2/{s/\(trn[0-9]*\)2/\13/g;}' yourfile

tikual
 
You can also do :

sed -e 's/\([tT][rR][nN]\)2452/\12453/' input >output

Jean Pierre.
 
Thanks for your help.
I used the post from tikual for my first question and the post from PHV for my second question.
Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top