sed puzzle

Nathan Aubrey nathan at paysonlinux.org
Wed Dec 5 10:54:39 MST 2007


On Wednesday 05 December 2007 10:25:30 you wrote:
> What about something like:
>
> $ cat file
> KIDS    :
> Mother  : Amanda
> Father  : Scott
>
> $ cat file | sed 's/:.*/: bob/g'
> KIDS    : bob
> Mother  : bob
> Father  : bob
>
> Not sure what you are trying to do, but the point is that using .* is
> probably what you want (it matches even if there is nothing in the field).
>
> -Charles
>
> Nathan Aubrey wrote:
> > All,
> >
> > 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?
> >
> > 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!

Currently, I try to get a variable of it
KIDS="`cat FILE | grep KIDS | cut -b10-`"
which gives me a blank here, so then I do
if [ "$KIDS" = "" \; then
  sed -i "s|KIDS     :|KIDS     : $KIDSNAMES|" FILE
else
  sed -i "s|$KIDS|$KIDSNAMES|" FILE
fi

But I was just curious if there was a single command I could use...

nathan


More information about the PLUG-discuss mailing list