Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

insert text to beginning of each word

17 views
Skip to first unread message

brumik

unread,
May 29, 2012, 5:16:31 PM5/29/12
to

I am trying to insert 2 characters followed by a space specifially "-D
" to the beginning of each word in a string and also to include the
first word in the string.

e..g have the strings separated by spaces:

"rpool/root rpool/var rpool/test"

I want to pipe this through sed to produce:

"-D rpool/root -D rpool/var -D rpool/test"

Could someone help how to do this please? How can I match each word
and insert -D in front of it.

Thanks in advance.

Chris F.A. Johnson

unread,
May 29, 2012, 6:22:48 PM5/29/12
to
Match the spaces:

str="rpool/root rpool/var rpool/test"
printf "%s\n" "$str" | sed 's/ / -D /g'

Or, with bash or ksh93:

printf "%s\n" "${str// / -D }"


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Janis Papanagnou

unread,
May 29, 2012, 11:24:25 PM5/29/12
to
Am 30.05.2012 00:16, schrieb brumik:
>
> I am trying to insert 2 characters followed by a space specifially "-D
> " to the beginning of each word in a string and also to include the
> first word in the string.
>
> e..g have the strings separated by spaces:
>
> "rpool/root rpool/var rpool/test"
>
> I want to pipe this through sed to produce:

Why sed?

>
> "-D rpool/root -D rpool/var -D rpool/test"
>
> Could someone help how to do this please? How can I match each word
> and insert -D in front of it.

printf " -D %s" rpool/root rpool/var rpool/test


Janis

>
> Thanks in advance.

Stephane Chazelas

unread,
May 30, 2012, 8:00:24 AM5/30/12
to
2012-05-29 14:16:31 -0700, brumik:
[...]

s/[^[:blank:]]\{1,\}/-D &/g

--
Stephane

romy

unread,
Jun 9, 2012, 2:42:14 PM6/9/12
to
try this -

cat text.file |sed "s/ / -D /g"

Ed Morton

unread,
Jun 12, 2012, 9:41:03 AM6/12/12
to
romy <rav...@gmail.com> wrote:

> try this -
>
> cat text.file |sed "s/ / -D /g"
>

Could you explain why using "cat" and a pipe as above is better than having
sed open the file:

sed "s/ / -D /g" text.file

Regards,

Ed.

Posted using www.webuse.net

Manuel Collado

unread,
Jun 12, 2012, 11:42:52 AM6/12/12
to
El 12/06/2012 15:41, Ed Morton escribi�:
> romy<rav...@gmail.com> wrote:
>
>> try this -
>>
>> cat text.file |sed "s/ / -D /g"
>
> Could you explain why using "cat" and a pipe as above is better than having
> sed open the file:
>
> sed "s/ / -D /g" text.file
>

That practice is so frequent that has a name: UUOC


> Regards,
> Ed.

I know you know it, just mentioned for the benefit of other readers.

Regards,
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

Ed Morton

unread,
Jun 12, 2012, 3:01:47 PM6/12/12
to
Manuel Collado <m.co...@domain.invalid> wrote:

> El 12/06/2012 15:41, Ed Morton escribiᅵ:
> > romy<rav...@gmail.com> wrote:
> >
> >> try this -
> >>
> >> cat text.file |sed "s/ / -D /g"
> >
> > Could you explain why using "cat" and a pipe as above is better than having
> > sed open the file:
> >
> > sed "s/ / -D /g" text.file
> >
>
> That practice is so frequent that has a name: UUOC

Yeah, I just thought maybe it'd have more lasting impact if we asked posters to
explain the usefulness of the "cat" so they thought about it themselves and came
to the right conclusion rather than just telling them up front that it was
useless. Perhaps this was a UUOP (Useless Use Of Posting) though. :-( .

Ed.

Posted using www.webuse.net

Janis Papanagnou

unread,
Jun 12, 2012, 3:37:10 PM6/12/12
to
On 12.06.2012 21:01, Ed Morton wrote:
> Manuel Collado <m.co...@domain.invalid> wrote:
>
[...]
>>
>> That practice is so frequent that has a name: UUOC
>
> Yeah, I just thought maybe it'd have more lasting impact if we asked posters to
> explain the usefulness of the "cat" so they thought about it themselves and came
> to the right conclusion rather than just telling them up front that it was
> useless. Perhaps this was a UUOP (Useless Use Of Posting) though. :-( .

No, it wasn't a UUOP; I think your approach is completely right!

Nonetheless we can observe that there are still "please help me to do better"
requests, and after some better suggestions, a "I prefer to continue doing it
the bad way" responses. - Now that is a waste of time! - if we only knew that
in advance. *sigh*

Janis

>
> Ed.
>
> Posted using www.webuse.net

Manuel Collado

unread,
Jun 13, 2012, 5:38:18 AM6/13/12
to
El 12/06/2012 21:01, Ed Morton escribiᅵ:
Well, my intention wasn't to explicitly tell that the above use of cat
if useless. Just mention a usual acronym for that practice, without
expanding it. In the hope that the OP will be intrigued by the acronym
and search the net for its meaning.

So I was just trying to reinforce your intention.
0 new messages