I have file with following content

<general id="7" src="xyz" srcType="abc" link="positive" />
<general id="3" src="xyz" srcType="pqr" link="positive" />

I am trying to replace every src name with its capitalize word. For example in above I want to replace all xyz with Xyz (first letter is capital).

Expected output is

<general id="7" src="Xyz" srcType="abc" link="positive" />
<general id="3" src="Xyz" srcType="pqr" link="positive" />

I tried regex pattern src="(.*?)" which can extract 'xyz' from the line but how can I use it with sed to replace in my file?

I tried following but it is not working

sed -E 's/srcName="(.*?)"/Xyz/1' filename

Bonus question, how can I generalize it? Where anything between scr=" and " can be converted into word capitalizing first character.


To edit XML, use an XML-aware tool. For example, there's xsh, a tool I adopted to maintain.

open file.xml ;
for //@src set . xsh:subst(., "(.)", "\U$1") ;
save :b ;
| improve this answer | |

A XML-aware tool, is definitely the way! For a quick hack:

in GNU sed:

sed -r 's/src="([a-z])/src="\U\1\E/g'  file.xml

for more complex changes I suggest Perl

perl -pe 's/src="\K(\w+)/ucfirst($1)/e'   file.xml


  • perl -pe command is a "readline, command, print" loop
  • s/regexp/expression/e substitutes regexp by the result of eval(expression)
  • src="\K is a left-context pattern
| improve this answer | |
  • I think you can drop the + from \w+ and just use s/src="\K(\w)/uc $1/e since it's only the first letter after the quote character we're interested in. – ilkkachu Jul 19 '19 at 11:58
  • Also, use \u in perl, there's no need to eval. – choroba Jul 19 '19 at 12:01
  • @ilkkachu, you are right. I was planning something more complex ucfirst(lc($1)) – JJoao Jul 19 '19 at 12:06
  • @choroba, you are right, That is equal to the sed version. In order to show the more powerful s///e I was planning perl -pe 's/src="\K(\w+)/ucfirst(lc($1))/e' , but that can still be done with \u operators. – JJoao Jul 19 '19 at 12:11

Tried with awk command and it worked fine

awk -F 'src="' '{$2=(toupper(substr($2,1,1))substr($2,2));$2="src="$2;print $0}' filename|sed 's/src=/&"/g'


<general id="7"  src="Xyz" srcType="abc" link="positive" />
<general id="3"  src="Xyz" srcType="pqr" link="positive" />
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.