Am 05. Dec, 2007 schwätzte Nathan Aubrey so:
moin moin,
> 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
$ ( newkid=anke; oldmom=Amanda; newmom=sabine; ( echo "KIDS :";
echo "Mother : Amanda" ) | sed -e "s@^\(KIDS[[:space:]]*:\).*@\1
$newkid@" -e "s@^\(Mother[[:space:]]*:[[:space:]]*\)$oldmom@\1$newmom@" )
KIDS : anke
Mother : sabine
$ ( newkid=anke; oldmom=Amanda; newmom=sabine; cat /tmp/Familie.txt | sed
-e "s@^\(KIDS[[:space:]]*:\).*@\1 $newkid@" -e
"s@^\(Mother[[:space:]]*:[[:space:]]*\)$oldmom@\1$newmom@" ) KIDS :
anke
Mother : sabine
Father : Scott
One recognizes lines without a name, the other lines with a name. The
pattern recognition that doesn't search for a name would also work when
there is a name.
$ ( newkid=anke; oldmom=Amanda; newmom=sabine; newdad=uwe; cat
/tmp/Familie.txt | sed -e "s@^\(KIDS[[:space:]]*:\).*@\1 $newkid@" -e
"s@^\(Mother[[:space:]]*:[[:space:]]*\)$oldmom@\1$newmom@" -e
"s@^\(Father[[:space:]]*:\).*@\1 $newdad@" )
KIDS : anke
Mother : sabine
Father : uwe
Lots of room to add error-checking.
> 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?
Multiple sed expressions, but you can toss them all on to one instance of
sed if you'd like.
> 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!
Learn the basics and then continue being amazed as you continue to learn
more basics... :)
ciao,
der.hans
--
# https://www.LuftHans.com/ http://www.CiscoLearning.org/
# A t-shirt a day keeps the noose away. - der.hans
---------------------------------------------------
PLUG-discuss mailing list -
PLUG-discuss@lists.plug.phoenix.az.us
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss