I have a string say, test_var=ab_c_de_fg_.txt
. I need to store ab_c
in one variable (characters before the 2nd _
) and de_fg_
in a second variable (characters after the second _
and before .txt
), so these two variables can be used in further operations.
test_var=ab_c_de_fg_.txt
for ((i=0;i<{#test_var};i++))
do
a[$i]="${var:$i:1}"
done
flag=0
temp=0
while [["$temp" -le "${#test_var}"]]
do
if a[temp] -eq "_" && flag -eq 0
flag = 1
continue
fi
if a[temp] -eq "_" && flag -eq 1
#var1=arr[0] to arr[$(temp-1)]
#var2=arr[$(temp+1)] to arr[#(test_var)]
fi
temp=$((temp+1))
done
Please guide me, since I am new to shell scripting. Thanks.
$
was added before{#test_var}
) – Stéphane Chazelas Jun 29 '20 at 09:29_
separator etc. Looks a bit like what someone might write in C to do that. – ilkkachu Jun 29 '20 at 09:31