sed puzzle

reed at reedmedia.net reed at reedmedia.net
Wed Dec 5 10:31:50 MST 2007


On Wed, 5 Dec 2007, Nathan Aubrey wrote:

> I need to replace some words with other words, but sometimes the words 
> are not present, so I need to add the words.
> In a config file I use, it has the following field
> 
> KIDS     :
> Mother   : Amanda
> Father   : Scott
> 
> 
> It is easy to swap out the names of mother or father, as the variables 
> are there, but if I try to swap out a kids name and there is no kids 
> name already in place, how do I add it instead? Is it one command, or 
> must I test if the content is empty then add instead of replacing?

If I understand you correctly, it would no different.


sed -e 's/^\(KIDS[^:]*: \).*$/\1Newname/'

Many ways to do this, but the above if there is or is not a name.

> The field example cannot change as it's just the format I use...
> I appreciate any insite, sed is a great tool, but man! what a beast!

  Jeremy C. Reed

p.s. I use sed constantly. Today, I wrote a simple script using sed to 
convert plain text to latex (special characters).

sed \
 -e 's/\\/BBaaCCkkSSllAAssHH/g' \
 -e 's/\#/\\#/g' \
 -e 's/\$/\\$/g' \
 -e 's/\%/\\%/g' \
 -e 's/\~/\\~/g' \
 -e 's/\_/\\_/g' \
 -e 's/\^/\\^/g' \
 -e 's/{/\\{/g' \
 -e 's/}/\\}/g' \
 -e 's/\	/\~\~\~\~/g' \
 -e 's/^ /\~/g' \
 -e 's/  /\~\~/g' \
 -e 's/^$/ \~ \\\\/g' \
 -e 's/BBaaCCkkSSllAAssHH/\\textbackslash{}/g' \
 -e 'G'	# double space


More information about the PLUG-discuss mailing list