SED: replace all the underscores in the first word only.

SED: replace all the underscores in the first word only.

am 11.01.2008 03:04:57 von Stuart

I start with this:

my_var_a my_var_a
my_var_b my_var_b

and want to get it to this:

my-var-a "'$my_var_a'"
my-var-b "'$my_var_b'"

But I'm really having trouble figuring out how to replace the
underscore characters in the first word/field. I would like to keep it
to using sed if possible. Any ideas? It might be really simple, since
I'm not that experienced with regexps.

Thanks :)

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 03:18:19 von Bill Marcum

On 2008-01-11, stuart wrote:
>
>
> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"
>
> But I'm really having trouble figuring out how to replace the
> underscore characters in the first word/field. I would like to keep it
> to using sed if possible. Any ideas? It might be really simple, since
> I'm not that experienced with regexps.
>
> Thanks :)

Why do you want to use sed? It would be easy with awk:
{gsub(/_/,"-",$1);print}
In sed, if the first word always contains two underscores:
s/\([^_]*\)_\([^_]\)_/\1-\2-/

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 03:37:55 von Icarus Sparry

On Thu, 10 Jan 2008 18:04:57 -0800, stuart wrote:

> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"
>
> But I'm really having trouble figuring out how to replace the underscore
> characters in the first word/field. I would like to keep it to using sed
> if possible. Any ideas? It might be really simple, since I'm not that
> experienced with regexps.
>
> Thanks :)

The basic idea is
1) make a copy of the line in the hold space
2) get rid of the first field
3) swap the hold space with the working space
4) get rid of everything beyound the first field.
5) change every underscore to a minus
6) append the hold space back onto the current space

If we make the assumption that the first field has no leading
spaces, and that it has at least one space after it, then the program is

h
s/^[^ ]*//
x
s/ .*//
s/_/-/g
G;s/\n//

If you can have tabs, or the first field might have leading spaces, then
you need slightly more complicated regular expressions. The second and
fouth lines become
s/^[ \t]*[^ \t]*//
s/\(^[ \t]*[^ \t]*\).*/\1/

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 05:53:29 von Stephane CHAZELAS

On Thu, 10 Jan 2008 18:04:57 -0800 (PST), stuart wrote:
> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"
>
> But I'm really having trouble figuring out how to replace the
> underscore characters in the first word/field. I would like to keep it
> to using sed if possible. Any ideas? It might be really simple, since
> I'm not that experienced with regexps.
[...]

sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
-e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"

--
Stephane

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 07:47:19 von mik3l3374

On Jan 11, 10:04 am, stuart wrote:
> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"
>
> But I'm really having trouble figuring out how to replace the
> underscore characters in the first word/field. I would like to keep it
> to using sed if possible. Any ideas? It might be really simple, since
> I'm not that experienced with regexps.
>
> Thanks :)


while read a b
do
a=`echo $a |sed 's/_/-/g' `
printf "$a .....\n" # you do the rest :)
done < file

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 08:21:10 von Stephane CHAZELAS

On Thu, 10 Jan 2008 22:47:19 -0800 (PST), mik3l3374@gmail.com wrote:
> On Jan 11, 10:04 am, stuart wrote:
>> I start with this:
>>
>> my_var_a my_var_a
>> my_var_b my_var_b
>>
>> and want to get it to this:
>>
>> my-var-a "'$my_var_a'"
>> my-var-b "'$my_var_b'"
>>
>> But I'm really having trouble figuring out how to replace the
>> underscore characters in the first word/field. I would like to keep it
>> to using sed if possible. Any ideas? It might be really simple, since
>> I'm not that experienced with regexps.
[...]
> while read a b
> do
> a=`echo $a |sed 's/_/-/g' `
> printf "$a .....\n" # you do the rest :)
> done < file

If you insist in using while read loops which are an offense to
goot shell scripting practice, at least do it properly:

read without -r is very special to the shell. If you use read,
you need to set IFS. Leaving a variable unquoted is very special
to the shell. echo is not a portable or reliable command, the
first argument of printf is a format, you shouldn't use variable
data for it.

unset IFS
while read -r a b
do
a=$(printf '%s\n' "$a" | sed 's/_/-/g')
printf "%s ...\n' "$a" # ...
done < file

But callng several utilities for each line of a file is not how
you should teach people to do shell scripting. And remember that
people pick recipes from articles on usenet so it's important to
be careful to what you write.

awk '
{
gsub(/_/, "-", $1)
$2 = "\"'\''$" $2 "'\''\""
print
}' < file

That's one command for the whole file.

--
Stephane

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 09:19:22 von mik3l3374

On Jan 11, 3:21 pm, Stephane Chazelas
wrote:
> On Thu, 10 Jan 2008 22:47:19 -0800 (PST), mik3l3...@gmail.com wrote:
> > On Jan 11, 10:04 am, stuart wrote:
> >> I start with this:
>
> >> my_var_a my_var_a
> >> my_var_b my_var_b
>
> >> and want to get it to this:
>
> >> my-var-a "'$my_var_a'"
> >> my-var-b "'$my_var_b'"
>
> >> But I'm really having trouble figuring out how to replace the
> >> underscore characters in the first word/field. I would like to keep it
> >> to using sed if possible. Any ideas? It might be really simple, since
> >> I'm not that experienced with regexps.
> [...]
> > while read a b
> > do
> > a=`echo $a |sed 's/_/-/g' `
> > printf "$a .....\n" # you do the rest :)
> > done < file
>
> If you insist in using while read loops which are an offense to
> goot shell scripting practice, at least do it properly:
>
> read without -r is very special to the shell. If you use read,
> you need to set IFS. Leaving a variable unquoted is very special
> to the shell. echo is not a portable or reliable command, the
> first argument of printf is a format, you shouldn't use variable
> data for it.
>
> unset IFS
> while read -r a b
> do
> a=$(printf '%s\n' "$a" | sed 's/_/-/g')
> printf "%s ...\n' "$a" # ...
> done < file
>
> But callng several utilities for each line of a file is not how
> you should teach people to do shell scripting. And remember that
> people pick recipes from articles on usenet so it's important to
> be careful to what you write.
>
> awk '
> {
> gsub(/_/, "-", $1)
> $2 = "\"'\''$" $2 "'\''\""
> print
> }' < file
>
> That's one command for the whole file.
>
> --
> Stephane


I suggest you chill for a bit.

Re: [ranting] SED: replace all the underscores in the first word only.

am 11.01.2008 09:40:37 von Stephane CHAZELAS

On Fri, 11 Jan 2008 00:19:22 -0800 (PST), mik3l3374@gmail.com wrote:
[...]
>> > while read a b
>> > do
>> > a=`echo $a |sed 's/_/-/g' `
>> > printf "$a .....\n" # you do the rest :)
>> > done < file
>>
>> If you insist in using while read loops which are an offense to
>> goot shell scripting practice, at least do it properly:
[...]
> I suggest you chill for a bit.
[...]

Sorry that you read it that way. May have sounded aggressive
because of the early hour in the morning, but

"while read" loops are really a plague. Except in very few cases
where they make sense, it's unreliable, ugly and inefficient.
I'm tired of correcting scripts written with them, so I'm
generally annoyed, especially first thing in the morning, when I
see them advertised on usenet.

--
Stephane

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 09:54:17 von mik3l3374

On Jan 11, 4:40 pm, Stephane Chazelas
wrote:
> On Fri, 11 Jan 2008 00:19:22 -0800 (PST), mik3l3...@gmail.com wrote:
>
> [...]>> > while read a b
> >> > do
> >> > a=`echo $a |sed 's/_/-/g' `
> >> > printf "$a .....\n" # you do the rest :)
> >> > done < file
>
> >> If you insist in using while read loops which are an offense to
> >> goot shell scripting practice, at least do it properly:
> [...]
> > I suggest you chill for a bit.
>
> [...]
>
> Sorry that you read it that way. May have sounded aggressive
> because of the early hour in the morning, but
>
> "while read" loops are really a plague. Except in very few cases
> where they make sense, it's unreliable, ugly and inefficient.
> I'm tired of correcting scripts written with them, so I'm
> generally annoyed, especially first thing in the morning, when I
> see them advertised on usenet.
>
> --
> Stephane

then shouldn't while loops be banned in bash? You shouldn't have to
get "annoyed" over such things. If the software doesn't enforce it the
way it "should be" , then you can't stop people using it the way they
like.
I am sure you have more important things to do than fret upon things
you can't control. enjoy your weekends

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 10:50:49 von Janis Papanagnou

On 11 Jan., 09:54, mik3l3...@gmail.com wrote:
> On Jan 11, 4:40 pm, Stephane Chazelas
> wrote:
>
> > "while read" loops are really a plague. Except in very few cases
> > where they make sense, it's unreliable, ugly and inefficient.
>
> then shouldn't while loops be banned in bash? You shouldn't have to
> get "annoyed" over such things. If the software doesn't enforce it the
> way it "should be" , then you can't stop people using it the way they
> like.

While loops cannot be (technically) banned because they are in the
bourne shells for decades and part of the standard. Then there are
also useful applications of while loops, as it had been mentioned.
We can, though, ban the while construct in cases where there its
application is unnecessary or there are better solutions available.
Not that I find the sed solution any better, frankly; rather see
Bill's awk suggestion upthread.

Janis

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 15:43:00 von Ed Morton

On 1/11/2008 2:54 AM, mik3l3374@gmail.com wrote:
> On Jan 11, 4:40 pm, Stephane Chazelas
> wrote:
>
>>On Fri, 11 Jan 2008 00:19:22 -0800 (PST), mik3l3...@gmail.com wrote:
>>
>>[...]>> > while read a b
>>
>>>>>do
>>>>> a=`echo $a |sed 's/_/-/g' `
>>>>> printf "$a .....\n" # you do the rest :)
>>>>>done < file
>>>>
>>>>If you insist in using while read loops which are an offense to
>>>>goot shell scripting practice, at least do it properly:
>>>
>>[...]
>>
>>>I suggest you chill for a bit.
>>
>>[...]
>>
>>Sorry that you read it that way. May have sounded aggressive
>>because of the early hour in the morning, but
>>
>>"while read" loops are really a plague. Except in very few cases
>>where they make sense, it's unreliable, ugly and inefficient.
>>I'm tired of correcting scripts written with them, so I'm
>>generally annoyed, especially first thing in the morning, when I
>>see them advertised on usenet.
>>
>>--
>>Stephane
>
>
> then shouldn't while loops be banned in bash?

That's a little like saying "since we should use hammers instead of screwdrivers
to hammer nails into wood, shouldn't screwdrivers be banned?". Stephane didn't
say "never use a while", he just said this isn't an appropriate application for
them.

> You shouldn't have to
> get "annoyed" over such things. If the software doesn't enforce it the
> way it "should be" , then you can't stop people using it the way they
> like.

So, your local hardware store shouldn't sell screwdrivers in case people buy one
to hammer in nails? No, all you can do is try to help them understand where
they're going wrong and suggest better alternatives, as Stephane did for you.

> I am sure you have more important things to do than fret upon things
> you can't control. enjoy your weekends

You could cut him some slack. He's just trying to help you and others reading
this group and he did appologise for coming on so strong.

Ed.

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 17:16:44 von mik3l3374

On Jan 11, 10:43 pm, Ed Morton wrote:
> On 1/11/2008 2:54 AM, mik3l3...@gmail.com wrote:
>
>
>
> > On Jan 11, 4:40 pm, Stephane Chazelas
> > wrote:
>
> >>On Fri, 11 Jan 2008 00:19:22 -0800 (PST), mik3l3...@gmail.com wrote:
>
> >>[...]>> > while read a b
>
> >>>>>do
> >>>>> a=`echo $a |sed 's/_/-/g' `
> >>>>> printf "$a .....\n" # you do the rest :)
> >>>>>done < file
>
> >>>>If you insist in using while read loops which are an offense to
> >>>>goot shell scripting practice, at least do it properly:
>
> >>[...]
>
> >>>I suggest you chill for a bit.
>
> >>[...]
>
> >>Sorry that you read it that way. May have sounded aggressive
> >>because of the early hour in the morning, but
>
> >>"while read" loops are really a plague. Except in very few cases
> >>where they make sense, it's unreliable, ugly and inefficient.
> >>I'm tired of correcting scripts written with them, so I'm
> >>generally annoyed, especially first thing in the morning, when I
> >>see them advertised on usenet.
>
> >>--
> >>Stephane
>
> > then shouldn't while loops be banned in bash?
>
> That's a little like saying "since we should use hammers instead of screwdrivers
> to hammer nails into wood, shouldn't screwdrivers be banned?". Stephane didn't
> say "never use a while", he just said this isn't an appropriate application for
> them.
>
> > You shouldn't have to
> > get "annoyed" over such things. If the software doesn't enforce it the
> > way it "should be" , then you can't stop people using it the way they
> > like.
>
> So, your local hardware store shouldn't sell screwdrivers in case people buy one
> to hammer in nails? No, all you can do is try to help them understand where
> they're going wrong and suggest better alternatives, as Stephane did for you.
>
> > I am sure you have more important things to do than fret upon things
> > you can't control. enjoy your weekends
>
> You could cut him some slack. He's just trying to help you and others reading
> this group and he did appologise for coming on so strong.
>
> Ed.

Just one example. You can't stop people using UUOC, even after you
tell them umpteen times that its "wrong". Even in published books, i
see alot of UUOC. After some time of helping people understand and
people still make the same "mistake" , you get used to it and let it
be. You come to know that you can't help it, because the tools are
there and up to the individual to use it the way they think its
right.. If I want to use a screwdriver to hit a nail, and it gets the
job done, then its right to me. If there's a universal ban of
everything in shell scripting that is wrong, wouldn't it be nice? then
people won't have to tell each other what is right and what is wrong.
That's why I suggested Stephane to enjoy his weekends. Life is too
short to dwell on these things he can't control.

Re: SED: replace all the underscores in the first word only.

am 11.01.2008 19:17:11 von Stuart

Thanks for all of your responses. I decided to base mine off of this:

sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
-e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"

Thanks. :)

On Jan 10, 8:53 pm, Stephane Chazelas
wrote:
> On Thu, 10 Jan 2008 18:04:57 -0800 (PST), stuart wrote:
> > I start with this:
>
> > my_var_a my_var_a
> > my_var_b my_var_b
>
> > and want to get it to this:
>
> > my-var-a "'$my_var_a'"
> > my-var-b "'$my_var_b'"
>
> > But I'm really having trouble figuring out how to replace the
> > underscore characters in the first word/field. I would like to keep it
> > to using sed if possible. Any ideas? It might be really simple, since
> > I'm not that experienced with regexps.
>
> [...]
>
> sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>
> --
> Stephane

Re: SED: replace all the underscores in the first word only.

am 13.01.2008 13:02:58 von William James

On Jan 11, 10:16 am, mik3l3...@gmail.com wrote:

> If I want to use a screwdriver to hit a nail, and it gets the
> job done, then its right to me. If there's a universal ban of
> everything in shell scripting that is wrong, wouldn't it be
> nice? then people won't have to tell each other what is right
> and what is wrong.

You drive nails with screwdrivers.
You insist you have the right to tell others to drive
nails with screwdrivers. You insist that no one else
has the right to tell others not to drive nails with
screwdrivers.

Keep drinking your Gatorade.

> That's why I suggested Stephane to enjoy his weekends. Life is
> too short to dwell on these things he can't control.

You can't control what Stephane posts. Stop dwelling on it.

--
Q: What's the most disgusting form of idiocy?
A: Arrogant idiocy.

Re: SED: replace all the underscores in the first word only.

am 13.01.2008 13:22:25 von William James

stuart wrote:
> Thanks for all of your responses. I decided to base mine off of this:
>
> sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"

You don't realize how hideous that is.

>
> Thanks. :)

Some guesses as to why you feel that you must
use sed.

1. You can't afford to buy awk.
2. You have heard that awk causes cancer in rhesus monkeys.
3. You fear that habitual use of awk may activate the neocortex.
4. Your mater will thrash you if you don't use sed.
5. Your I.Q. is equal to that of mik3l3. That may explain the
perennial rictus.

Re: SED: replace all the underscores in the first word only.

am 13.01.2008 15:39:12 von joe

stuart writes:

> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"
>
> But I'm really having trouble figuring out how to replace the
> underscore characters in the first word/field. I would like to keep it
> to using sed if possible. Any ideas? It might be really simple, since
> I'm not that experienced with regexps.

I didn't see this suggested:

#!/bin/bash

output()
{
set -- $@
printf "%s \"'%s'\"\n" $1 $2
}

output my_var_a my_var_a
output my_var_b my_var_b

sed seems really overkill to me for something like this

Joe
--
To email me shift the letters in my address back one

Re: SED: replace all the underscores in the first word only.

am 13.01.2008 15:56:59 von joe

Joe writes:

> stuart writes:

> #!/bin/bash
>
> output()
> {
> set -- $@
> printf "%s \"'%s'\"\n" $1 $2
> }
>
> output my_var_a my_var_a
> output my_var_b my_var_b

Ahh, missed the first part, sorry.

#!/bin/bash

switchUnderscore()
{
OLDIFS="$IFS"
IFS='_'
set -- $@
printf "%s-%s-%s" $1 $2 $3
IFS="$OLDIFS"
}

output()
{
set -- $@
first=`switchUnderscore $1`
printf "%s \"'$2'\"\n" $first
}

output my_var_a my_var_a
output my_var_b my_var_b

Joe
--
To email me shift the letters in my address back one

Re: SED: replace all the underscores in the first word only.

am 14.01.2008 03:06:35 von mik3l3374

On Jan 13, 8:02 pm, William James wrote:
> On Jan 11, 10:16 am, mik3l3...@gmail.com wrote:
>
> > If I want to use a screwdriver to hit a nail, and it gets the
> > job done, then its right to me. If there's a universal ban of
> > everything in shell scripting that is wrong, wouldn't it be
> > nice? then people won't have to tell each other what is right
> > and what is wrong.
>
> You drive nails with screwdrivers.
> You insist you have the right to tell others to drive
> nails with screwdrivers. You insist that no one else
> has the right to tell others not to drive nails with
> screwdrivers.
>

Since when did I insist on that? If you are bright enough to read the
posts so far, i am not the one who started this
crap.

> Keep drinking your Gatorade.
>
> > That's why I suggested Stephane to enjoy his weekends. Life is
> > too short to dwell on these things he can't control.
>
> You can't control what Stephane posts. Stop dwelling on it.
>
> --

That would have happened if you did not come in again to rub the
wound ..

Re: SED: replace all the underscores in the first word only.

am 14.01.2008 03:08:21 von mik3l3374

On Jan 13, 8:22 pm, William James wrote:
> stuart wrote:
> > Thanks for all of your responses. I decided to base mine off of this:
>
> > sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
> > -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>
> You don't realize how hideous that is.
It may look hideous to you, but may not to others. What makes you
think you have the right to judge what's not and what is?

>
>
>
> > Thanks. :)
>
> Some guesses as to why you feel that you must
> use sed.
>
> 1. You can't afford to buy awk.
> 2. You have heard that awk causes cancer in rhesus monkeys.
> 3. You fear that habitual use of awk may activate the neocortex.
> 4. Your mater will thrash you if you don't use sed.

If OP wants to use sed, is that any of your business?

> 5. Your I.Q. is equal to that of mik3l3. That may explain the
> perennial rictus.

Your's is way below OP's .

Re: SED: replace all the underscores in the first word only.

am 14.01.2008 16:06:15 von Janis Papanagnou

mik3l3374@gmail.com wrote:
> On Jan 13, 8:22 pm, William James wrote:
>>
>>>sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
>>> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>>
>>You don't realize how hideous that is.
>
> It may look hideous to you, but may not to others.

You are not serious, are you?

Janis

Re: SED: replace all the underscores in the first word only.

am 14.01.2008 16:52:48 von Stephane CHAZELAS

On Mon, 14 Jan 2008 16:06:15 +0100, Janis Papanagnou wrote:
> mik3l3374@gmail.com wrote:
>> On Jan 13, 8:22 pm, William James wrote:
>>>
>>>>sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
>>>> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>>>
>>>You don't realize how hideous that is.
>>
>> It may look hideous to you, but may not to others.
>
> You are not serious, are you?
[...]

Note that the gawk equivalent:

gawk '
{
while ((rep = gensub(/^([^[:blank:]]*)_/, "\\1-", "")) != $0)
$0 = rep;
$0 = gensub(/([[:blank:]]+)(.*)/, "\\1\"'\''$\\2'\''\"", "")
print
}'

is not a lot better.

The other awk solutions that have been given, IIRC don't respect
spacing.

An awk implementation that respects spacing could be:

awk '
match($0, /[[:blank:]]+/) {
first = substr($0, 1, RSTART - 1)
sep = substr($0, RSTART, RLENGTH)
second = "\"'\''$" substr($0, RSTART + RLENGTH) "'\''\""
gsub(/_/, "-", first)
$0 = first sep second
}
{print}'

--
Stephane

Re: SED: replace all the underscores in the first word only.

am 14.01.2008 18:53:44 von Ed Morton

On 1/14/2008 9:52 AM, Stephane Chazelas wrote:
> On Mon, 14 Jan 2008 16:06:15 +0100, Janis Papanagnou wrote:
>
>>mik3l3374@gmail.com wrote:
>>
>>>On Jan 13, 8:22 pm, William James wrote:
>>>
>>>>>sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
>>>>> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>>>>
>>>>You don't realize how hideous that is.
>>>
>>>It may look hideous to you, but may not to others.
>>
>>You are not serious, are you?
>
> [...]
>
> Note that the gawk equivalent:
>
> gawk '
> {
> while ((rep = gensub(/^([^[:blank:]]*)_/, "\\1-", "")) != $0)
> $0 = rep;
> $0 = gensub(/([[:blank:]]+)(.*)/, "\\1\"'\''$\\2'\''\"", "")
> print
> }'
>
> is not a lot better.
>
> The other awk solutions that have been given, IIRC don't respect
> spacing.
>
> An awk implementation that respects spacing could be:
>
> awk '
> match($0, /[[:blank:]]+/) {
> first = substr($0, 1, RSTART - 1)
> sep = substr($0, RSTART, RLENGTH)
> second = "\"'\''$" substr($0, RSTART + RLENGTH) "'\''\""
> gsub(/_/, "-", first)
> $0 = first sep second
> }
> {print}'
>

All true in theory, but as we know: Theory and practice are only equivalent in
theory. In practice, they're quite different.

In practice, the OP almost certainly doesn't care about the white space or just
has one blank between fields as shown in his posted input:

> I start with this:
>
> my_var_a my_var_a
> my_var_b my_var_b
>
> and want to get it to this:
>
> my-var-a "'$my_var_a'"
> my-var-b "'$my_var_b'"

so the practical awk solution for THIS problem as posted is probably just:

awk -v s=\' -v d=\" '{gsub(/_/,"-",$1); $2=d s "$" $2 s d}1' file

Ed.

Re: SED: replace all the underscores in the first word only.

am 15.01.2008 00:30:28 von Janis Papanagnou

Stephane Chazelas wrote:
> On Mon, 14 Jan 2008 16:06:15 +0100, Janis Papanagnou wrote:
>
>>mik3l3374@gmail.com wrote:
>>
>>>On Jan 13, 8:22 pm, William James wrote:
>>>
>>>>>sed -e :1 -e 's/^\([^[:blank:]]*\)_/\1-/;t1' \
>>>>> -e "s/\([[:blank:]]\{1,\}\)\(.*\)/\1\"'\$\2'\"/"
>>>>
>>>>You don't realize how hideous that is.
>>>
>>>It may look hideous to you, but may not to others.
>>
>>You are not serious, are you?
>
> [...]
>
> Note that the gawk equivalent:
>
> gawk '
> {
> while ((rep = gensub(/^([^[:blank:]]*)_/, "\\1-", "")) != $0)
> $0 = rep;
> $0 = gensub(/([[:blank:]]+)(.*)/, "\\1\"'\''$\\2'\''\"", "")
> print
> }'
>
> is not a lot better.

Right. But I was not talking about equivalence of a regexp solution
in tool A compared to a regexp solution in tool B.

The (IMO much better) awk solution[*] made a complex and error-prone
regexp use obsolete by decomposing the task to two much more simple
programming patterns; field splitting and a simple substitution, both
standard (and non-obfuscating!) operations in awk.

[*] I mean the awk solution that Bill posted and which you corrected
slightly by introducing quotes, somewhere upthread.

>
> The other awk solutions that have been given, IIRC don't respect
> spacing.

It might be the case that spacing is relevant in some requests, but
I don't see any evidence that it is in this case. The OP might tell
us whether space preservation is necessary.

Janis

>
> An awk implementation that respects spacing could be:
>
> awk '
> match($0, /[[:blank:]]+/) {
> first = substr($0, 1, RSTART - 1)
> sep = substr($0, RSTART, RLENGTH)
> second = "\"'\''$" substr($0, RSTART + RLENGTH) "'\''\""
> gsub(/_/, "-", first)
> $0 = first sep second
> }
> {print}'
>