string manipulation

Hello,

I have a korn shell string variable

str1 = "A,B,Z"

I would like to create another korn shell string variable

str2 = "letter = 'A' or letter = 'B' or letter = 'Z' "

Please help!

Thanks in advance

an UNIX newbie!

Are you looking for something like this:

str1="A,B,Z"
str2=`echo $str1 | cut -d, -f1`

Use -f2 for letter B and -f3 for letter Z.

Thanks for quick response!

The problem is that str1 may contains any number of letters separated by a comma

I think I need some sort of loop!

can you please be more precise?? like which value do yo have to pick. is it the first one the second one or the last one?

One way to do that:

str1="A,B,C,D,E,F,G,H,I"
count=`echo $str1 | awk -F, '{print NF}'`
i=2
while ((i <= $count));
do
str${i}=`echo $str1 | cut -d, -f${i}`
(( i = i + 1));
done

Another way, using awk arrays:

str1="A,B,C,D,E,F,G,H,I,J"
echo $str1 | awk -F, '{
for (i =1; i <= NF; i++)
         a=$i;
}'

You can reference array subscript later on to access values.

Regards,
Tayyab

Thanks Tayyab!

I tried your second suggestion but one more stupid question: array a is in awk, how do I reference to it in Shell.

Regards

eval `echo $str1 | awk -F, '{
for (i =1; i <= NF; i++)
        printf("a[%d]=%s\n",i,$i);
}'`

Use the above code to access array "a" in shell script.

Hi,

I have a korn shell string variable like with the value

str1 = "first\nsecond\nthird\lnfourth"

I would like to cut the string based on the "\n" character, wherever occur the "\n" character in the line.

how can we split the string based on the "\n" character like this...?

s1= "first"
s2="second"
s3="third"
s4="fourth"

Please help!

Thanks in advance
Siva.P
Bangalore
India.

Continuing with anbu's code. use \n as the field separator.

eval `echo $str1 | awk -F"\n" '{
for (i =1; i <= NF; i++)
        printf("a[%d]=%s\n",i,$i);
}'`

Hi Ahmed,

The beloew code is not generating any output.

eval `echo $str1 | awk -F"\n" '{
for (i =1; i <= NF; i++)
printf("a[%d]=%s\n",i,$i);
}'`

Regards,
Siva.P
Bangalore
India.

you need to assign the value to the variable str1 before running my code

Try follow code:

str="a,b,c,d,e"
echo $str | awk 'BEGIN{FS=","}
{for (i=1;i<=NF;i++)
{if (i==1)
$i=sprintf("letter=|%s|",$i)
else$i=sprintf(" or letter=|%s|",$i)
}
print $0}
END{print str1}'

But i do not know how to print <'> in awk body, so i use <|> instead.

Anyone can help me on this will be very appreciate.

Hi ahemad,

i have updated the above script and tested but its not generating any outputs..
See by updated code..

str1 = "first\nsecond\nthird\lnfourth"
echo "Actual Given String is : $str1"
eval `echo $str1 | awk -F"\n" '{
for (i =1; i <= NF; i++)
printf("a[%d]=%s\n",i,$i);
}'`

Thanks in Advance and help me any one.

Regards,
Siva.P
Bangalore
India.

$ s="first\nsecond\nthird\nfourth"
$ set -- $(print $s)
$ printf "\$1 is %s\n\$2 is %s\n...\n" $1 $2
$1 is first
$2 is second
...

Then you can:

$ c=1;while [ $c -le ${#@} ];do eval "s$c=\${$c}";c=$((c+1));done
$ print $s1
first
$ print $s4
fourth