I want to split this into columns in order to load in database, anything starts with"&mjv=6" as first column &rel=5 as second column,&oxi=2 as third column like wise...for entire param list. But all values in parameters may change. So I guess i can use wildcard, but i need to use "&" as delimiter.
#!/bin/bash
while IFS='&' read -a L
do
for i in ${!L[@]}; do
eval "${L[$i]}"
done
for V in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
do
echo -en "$V=${!V}\t"
done
echo
done <infile
while IFS='&' read -a L
# set the field separator to '&' and read the line as an array
# each elemnt of te array corresponds to an axpression between '&'
# L[0]="poc=4060", L[1]="mjv=6", L[2]="mov=5" and so on
# The line is plitted into an array with '&' as separator.
do
for i in ${!L[@]}; do # for each index in the array
eval "${L[$i]}" # "evaluate", which means execute the command in the string
done
for V in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
do
echo -en "$V=${!V}\t" # ${!<variable>} is indirect reference
done
echo
done <infile
for i in ${!L[@]}; do
eval "${L[$i]}"string
done
could also be done like
for E in ${L[@]}; do # for each element of the array
eval "$E"
done
The use of eval to execute input data is dangerous stuff because it allows a malicious user with access to the data file to execute any code as the user who executes the script.
Alternative shell script:
# oldIFS=$IFS
IFS='&'
while read line
do
for f in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
do
for i in $line
do
case $i in
$f=*) printf "%s\t" "$i"
esac
done
done
echo
done <infile
# IFS=$oldIFS
Could this help ? #!/bin/bash
while read L
do
for V in poc ooc mjv omj mov ps rel bod oxi ozn lang loc drc
do
Val=$(echo "$L" | grep -oE "$V=[^&]" | sed 's/.=//')
echo -en "$V=${Val:-NULL}\t"
done
echo
done <$INFILE
# oldIFS=$IFS
IFS='&'
while read line
do
for f in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
do
val=
for i in $line
do
case $i in
$f=*) val=$i; break
esac
done
printf "%s\t" "${val:-$f=NULL}"
done
echo
done <infile
# IFS=$oldIFS