How to split the string value to an array?

Test1.txt

    Tom is hot
    

Test.sh

     filename="/directory/Test1.txt"
     set - A store
     while IFS= read value
     do
          awk '{split($value,store," ")}'
     done < "$filename"
     echo ${#sore[@]}
     

From the code in the executing file, I would like each word in each line to be stored in an array. Later I would like the output to be printed out like below with more lines in the content, but each column of the line is limited to 3.
Subject Verb Adjective
Tom is hot
Jane is sweet
..............................................

I tried executing the code, but it seems that no value is stored into the array....I printed out the number of array in store and it shows 0....... How to split the string and append into the array?

php -a   # enter interactive mode in terminal for testing 
php > $line = "Tom is hot";
php > $array = explode(' ',$line);
php > print_r($array);
Array
(
    [0] => Tom
    [1] => is
    [2] => hot
)
php > 

Hi
Maybe so

store=($(<$filename))
$python
>>> line = "Tom is hot"
>>> array = line.split(' ')
>>> print(array)
['Tom', 'is', 'hot']

associative array

declare -A store
eval read store[{$(fmt -1 $filename | paste -sd,)}] < <(seq -s' ' $(wc -w <$filename))
echo ${store[@]}
echo ${!store[@]}

--- Post updated at 10:19 ---

another way to write to an associative array. This method is more flexible.

declare -A store
eval store=($(awk '{print "[" $0 "]=" NR}' RS='[[:space:]]+' $filename))
echo ${store[@]}
echo ${!store[@]}
1 Like

Hey nezabudka ,

Please include the input and output (strings / text) in your code samples, so we can see exactly your input and outputs.

Keep up the good work and....

Thanks!

1 Like

We can build a matrix!

declare -A store store2
eval read store[$(awk 'END {print "{1.." NR "},{1.." NF "}"}' $filename)] < <(fmt -1000 $filename)
for i in {1..2},{1..3}; do echo store[$i] = ${store[$i]}; done

If the number of columns is not constant for all lines

eval read store2[$(awk '{if(NF>max) max=NF} END {print "{1.." NR "},{1.." max "}"}' $filename)] < <(fmt -1000 $filename)
GNU bash, version 5.0.11(1)-release (x86_64-redhat-linux-gnu)

Or so it's easier and more understandable matrix creating

declare -A store
eval store=($(awk '{for(i=1;i<=NF;i++) print "[" NR "," i "]=" $i}' $filename))

It should work the same way on both the "bash" and the "ksh93"

cat file
tom is hot
one two three
four five six
set -A store
#declare -A store
eval "store=($(awk '{for(i=1;i<=NF;i++) print "[" NR "." i "]=" $i}' file))"

print first line. you can even take ten with a margin

eval echo '${'store[1.{1..10}]}
tom is hot

revers

eval echo '${'store[1.{10..1}]}
hot is tom

output 2 columns

eval echo '${'store[{1..10}.2]}
is two five

odd lines

eval echo '${'store[{1..10..2}.{1..10}]}
tom is hot
four five six

#Change case of all first characters of the first line (only bash. I just do not know the options for ksh93)

#eval echo '${'store[1.{1..10}^]
#Tom Is Hot

print only the first characters of all values

eval echo '${'store[{1..10}.{1..10}]:0:1}
t i h o t t f f s

print only the last characters of all values

eval echo '${'store[{1..10}.{1..10}']:\(-1\)}'
#eval echo \${store[{1..10}.{1..10}]:\(-1\)} #for bash
m s t e o e r e x

leave one character in words with an odd number of letters

eval echo '${'store[{1..10}.{1..10}]//??} #for visibility replace with a plus //??/+}
m t e o e x

And here accuracy is already needed. Determine the length of each value

eval echo '${'#store[{1..3}.{1..3}]}
3 2 3 3 3 5 4 4 3
2 Likes