On Mon, 31 Jul 2000 [EMAIL PROTECTED] wrote:

>On Mon, Jul 31, 2000 at 04:10:43PM -0400, Mike A. Harris wrote:
>> Is there an easy way using bash INTERNALS to do the following:
>> 
>> Read in a file containing two numbers on one line separated by
>> space, and replace the whitespace between them with a ":"?
>> 
>> I'm trying to optimize some stuff in a script and trying to get
>> rid of awk/sed/perl calls that are unnecessary.
>> 
>> The input file contains:
>> 
>> 2345 5678
>
>try something like this:
>
>$ zz="2345        5678"
>$ echo ${zz%% *}:${zz##* }
>2345:5678
>
>I tried fiddling with tabs in the whitespace but couldn't figure it
>out, so I don't know if that will work or not. If it's just spaces,
>this might work for you.

Ok great!  I tried that with your example and it works, but it
didn't work with the input I have.  I have hex displayed the
input, and it contains a 0x09 which is a HTAB.  I've changed the
code to:

echo -${zz%%    *}:${zz##*     }

This produces:
2345:5678

The big space is a literal tab character that I entered by
pressing CTRL-V TAB.  When I use this, it works
perfectly.  However, such code is very unreadable and
confusing.  I tried replacing the hard TAB character with "\t",
"\011", all to no avail.  Any other idea?

echo -e ${zz%%\t*}:${zz##*\t}

Produces:
2345    5678:2345    5678

echo -e ${zz%%\011*}:${zz##*\011}

Produces the same.  Putting it all in "" marks does the same as
well.

>The '%%' deletes everything that matches the pattern (in this case, a
>space followed by anything) from the end of the variable's value.
>
>The '##' does the opposite: deletes anything (followed by a space)
>from the beginning of the variable's value.

Wow.  You just explained it in a way that makes total sense, and
in about 1/3 the words used in the cryptic bash manpage.  I read
and reread the manpage several times and had no idea what it was
meaning, and I'm pretty good with technical reading...

Perhaps you should maintain the bash manpage.  ;o)

I'm at least on the right track now.  Thanks a bundle.
TTYL

-- 
Mike A. Harris                                     Linux advocate     
Computer Consultant                                  GNU advocate  
Capslock Consulting                          Open Source advocate

... Our continuing mission: To seek out knowledge of C, to explore
strange UNIX commands, and to boldly code where no one has man page 4.



_______________________________________________
Redhat-devel-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

Reply via email to