Wednesday 4 March 2020

Bash script : How to split string to array and count/print array elements based on index

IFS    The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.


Example 1 :

#!/bin/bash
fvt_test_arg="DEFAULT HCOLL IBV PAMI_NOIB"
echo "This is fvt_test_arg : $fvt_test_arg"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
echo "This is fvt_test_arg_array : ${fvt_test_arg_array[@]}"
echo "Number of elements in the array : ${#fvt_test_arg_array[@]}"
echo "1st element is :${fvt_test_arg_array[0]}"
echo "2nd element is :${fvt_test_arg_array[1]}"
echo "3rd element is :${fvt_test_arg_array[2]}"
echo "4th element is :${fvt_test_arg_array[3]}"


----------------------------
OUTPUT:

[sachin@host1 myscripts]$ ./list4.sh
This is fvt_test_arg : DEFAULT HCOLL IBV PAMI_NOIB        ===> String
This is fvt_test_arg_array : DEFAULT HCOLL IBV PAMI_NOIB  ====> array
Number of elements in the array : 4
1st element is :DEFAULT
2nd element is :HCOLL
3rd element is :IBV
4th element is :PAMI_NOIB
[sachin@host1 myscripts]$

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EXAMPLE 2:

#!/bin/bash
fvt_test_arg="DEFAULT HCOLL IBV PAMI_NOIB"
echo "This is fvt_test_arg : $fvt_test_arg"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
echo "This is fvt_test_arg_array : ${fvt_test_arg_array[@]}"
echo "Number of elements in the array : ${#fvt_test_arg_array[@]}"
echo "1st element is :${fvt_test_arg_array[0]}"
echo "2nd element is :${fvt_test_arg_array[1]}"
echo "3rd element is :${fvt_test_arg_array[2]}"
echo "4th element is :${fvt_test_arg_array[3]}"

pids="1111 2222 3333 4444"
i=0
for pid in ${pids[@]}; do
    echo " $pid is the PID of the process for :${fvt_test_arg_array[i]} is successful"
    i=$((i+1))
done

 ---------------
OUTPUT:
[sachin@host1 myscripts]$ ./list5.sh
This is fvt_test_arg : DEFAULT HCOLL IBV PAMI_NOIB
This is fvt_test_arg_array : DEFAULT HCOLL IBV PAMI_NOIB
Number of elements in the array : 4
1st element is :DEFAULT
2nd element is :HCOLL
3rd element is :IBV
4th element is :PAMI_NOIB
 1111 is the PID of the process for :DEFAULT is successful
 2222 is the PID of the process for :HCOLL is successful
 3333 is the PID of the process for :IBV is successful
 4444 is the PID of the process for :PAMI_NOIB is successful
[smpici@c712f6n06 myscripts]$


------------------------------------
 EXAMPLE  3:

#!/bin/bash

declare -a num
declare -a words

num=(ddd1 dd2 dd3 dd4 dd5 dd6 dd7)
words=(one two three four five six seven)

n=${#num[@]}
echo $n
for ((i=0;i<$n;i++)); do
  echo ":${num[$i]} :${words[$i]}:"
done
[sachin@host1 myscripts]$

--------------------------

OUTPUT:
[sachin@host1 myscripts]$ ./list3.sh
7
:ddd1 :one:
:dd2 :two:
:dd3 :three:
:dd4 :four:
:dd5 :five:
:dd6 :six:
:dd7 :seven:
-----------------------------------------------

Reference:
https://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash/13196466

No comments:

Post a Comment