Hi all,
Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character?
Example:
var1=ktest1 will become var1=test1
var2=rtest2 will become var2=test2
Need help please.
hergp
April 7, 2010, 2:36am
4
frans solution only works in ksh93, alisters also in ksh88
# echo "ktest1" | sed "s/^.\(.*\)/\1/"
test1
[root@sistem1lnx passwd]# cat delfirstchar
#!/bin/bash
echo "$*"|grep -o ^. > file
n=0
while read cr;
do
if [ $n -gt 0 ] ; then
crx+=$cr
sleep 1
else
let n+=1
continue
fi
done < file
echo $crx
[root@sistem1lnx passwd]# ./delfirstchar ktest1 && ./delfirstchar rtest2
test1
test2
Jairaj
April 7, 2010, 7:35am
7
Try This:
echo "var1=ktest1" | awk -F'=' '{print substr($2,2)}'
---------- Post updated at 06:35 AM ---------- Previous update was at 06:01 AM ----------
Try this:
echo "var1=ktest1" | awk -F'=' '{print substr($2,2)}'
hergp -
alister's answer works for POSIX-compliant shells - i.e., most modern shells - among them are ksh, zsh, and bash
Check out The Open Group - Making Standards Work
See section 2.6.2 Parameter Expansion under 'The Shell Command Language'
Any shell that meets all the requirements listed by opengroup.org is consider POSIX-compliant.
hergp
April 7, 2010, 8:02am
9
You are right, Jim! Guess I am to much of a ksh-o-holic
kshji
April 7, 2010, 10:56am
10
Or this, works only in ksh93, bash, zsh, not in dash or ksh88.
var="${var/?/}" # replace first some char with nothing
var1=` echo $var1 | awk '{print substr($1,2)}'`