Removing a character from a variable and assigning it to another variable?

Hi folks.

I have this variable called FirstIN that contains something like this: 001,002,003,004...

I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces)

I thought about using sed but i am not getting the expected result...which i guess is due to my misunderstanding of the command...

Maybe with tr instead? How would i copy the content of the original var into the second var while using tr? tried ModifiedIN=$FirstIN | tr ',' ' ' but that is wrong...

Thanks.

ModifiedIN=$( echo $FirstIN  | sed "s/,/ /g" )
ModifiedIN=$( echo $FirstIN  | sed "y/,/ /" )
ModifiedIN=$( echo $FirstIN  | tr ',' ' ')

DOH! The echo! Thanks anbu23.

# FirstIN=001,002,003,004
# echo $FirstIN | tr ',' ' '
001 002 003 004
# FirstINa=`echo $FirstIN | tr ',' ' '`
# echo $FirstINa
001 002 003 004

---------- Post updated at 01:50 PM ---------- Previous update was at 01:48 PM ----------

Or using shell builtin parameter expansion

# FirstINb=${FirstIN//,/ }
# echo $FirstINb
001 002 003 004

What shell does that work for? It doesn't work for me in KSH.

$ FirstIN=001,002,003,004

$ echo $FirstIN
001,002,003,004

$ FirstINb=${FirstIN//,/ }
ksh: : bad substitution

It's a bashism.

It's not a bash-ism. It works fine in a bourne shell. I even tested it in busybox ash, one of the most limited, stripped-down, barebones bourne shells still available today.

FirstIN=001,002,003,004
ModIN=$(echo "$FirstIN" | tr "," " ")

or

ModIN=`echo "$FirstIN" | tr "," " "`

...But if I recall correctly it is still a POSIX shell. Older ksh and true bourne shell (solaris) probably won't know what to do with it.

That would explain why it even works in vanilla ksh when I finally caved in and installed it. Solaris still ships with non-POSIX shells? How lovely to get accused of BASH-ism for following POSIX...

Where can I find such a truly garbage shell to test with, to ensure code we suggest will work with solaris? :rolleyes:

On Solaris :slight_smile:

Even the sun guys find it odd but in Solaris backward compatibility is king, so taking anything out once it's there is hard. There are POSIX shells on Solaris also but probably the most common environment that I see for a POSIX shell snippet not working in a given environment is Solaris.

You are quite correct.

KSH93 supports it (perhaps it originated there?), but KSH88 does not. I suspect that SFNYC, like myself, is using pdksh which is KSH88 for the most part.

For the sticklers, it still hasn't found its way into the POSIX sh standard.

Thanks for the correction, Corona688.

Regards,
Alister

Interesting. It wasn't working for me on Linux using

$ echo $SHELL
/bin/ksh

$ echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2

So, is this version of KSH not POSIX-compliant?

pdksh is mostly ksh88 and that feature was not introduced in ksh until ksh93.

POSIX compatibility is irrelevant as this type of parameter expansion is NOT in the POSIX standard.

See Section 2.6.2 of Shell Command Language

Regards,
Alister

Dear Friend,

You can execute the following in the command line

 step 1: 
 var="100,200,300"
 
 step 2: 
var1=`echo $var | sed -e 's/,/ /g'`
echo $var1
 

The output is
100 200 300

Older ksh88 version is not Posix compatible, because posix has done later. Most of commercial *nix still include only ksh88 version.
But you can download also ksh93 version, which is posix-compatible. Why ? I think that Posix-team has used lot of ksh93 to make posix standard.
Download binary.

Simple test, ksh88 or ksh93. Many *sh based on ksh88 ...
Ex. substr not work in older shells.

abc="1234"
substr=${abc:1:2}
echo  "<$substr>"

Posix
Utils

Hi, kshji

I don't understand how you come to the conclusion that ksh93 is somehow more posix-y than ksh88. If you take a look at the features it introduced (associative arrays, floating point arithmetic, new forms of parameter expansion, etc), 15 years later they're not part of the posix sh standard. If anything, ksh93 deviates further from the standard.

The example you chose, substring parameter expansion, exemplifies this. It is a feature that's supported by most modern variants of the bourne shell, but it is not part of posix. Any shell that does not implement it is not any less posix-compliant because of it. The same goes for the pattern/replacement parameter expansion that started this discussion. (Though they are both handy features, no doubt.)

I don't know about you, but i love shell chat :wink:

Regards,
alister

You are right.
Shell Command Language
I didn't check... I have learned this substr feature, not from ksh, it was some "posix-sh". Now I know, it wasn't :). Or it was - include posix.

Dash is maybe the most of posix-compatible and only posix. I know that ksh and many other "Bourne shell child" include lot of extension features. Much more than posix shell. For me posix shell compatible means that sh include posix. I try to use only posix stuff (almost :).

Anyway, parameter extension is powerful, but not so easy to read. Need always little comments for others, who are not so sh gurus. sed, tr, cut, awk, expr, .. lines are more readable, but need huge cpu if compare using of parameter extension. Not built-in.

So, my test was simple test to compare, have you ksh88 or ksh93. Nothing else.

Sorry, english it's not my main language.