Advanced sed find and replace
19 Jan 2010
I always forget how to do this, so, by god, I'm going to actually write it down this time so that I don't have to search all over the place next time.
To find a string of text and replace it with part of the original string intact:
sed -i 's/\(foo\)\([A-Z, -]*\)/\1\L\2/g' file.txt
Putting things in escaped parentheses creates a group—\(foo\) is group one, \([A-Z]*\) is group two. Then in the replace string, you call up a group by putting \#. So group 1 becomes \1. Thus, the above replacement string—\1\L\2—means keep group one as is but convert group two, whatever it might be, to lowercase.
No Comments
No comments yet.
RSS feed for comments on this post.
Leave a comment