Hi, I have a file with content many rows, some of those is incorrect data. I want to re-calculator the data, do it with right data, and after that overwrite row line in files. I not familiar with shell, but I know some basic of them. So, I just ask about algorithm or code to resolve it. Look my file and some script shell I build.
Name=John
ID=1 2 3 4
Age=32
IDCode1=1 2 3
IDCode1Qty=5 3 2
IDCode1Loc=1 3 2 4 5 1 2 3 2 1
IDCode2=2 3 4
IDCode2Qty=3 3 1
IDCode2Loc=4 6 5 3 4 5 1
IDCode3=1 3 4
IDCode3Qty=1 1 5
IDCode3Loc=6 6 2 3 4 6 5
The data at line IDCodeLocation is wrong , it based on ID, IDcode and IDcodeQty
lines. The result should be like this, I will explain more detail in file
Name=John
ID=1 2 3 4 > total ID is 4
Age=32
IDCode1=1 2 3 > follow id from ID
IDCode1Qty=5 3 2
IDCode1Loc=1 2 3 4 5 1 2 3 1 2 > data is correct
1 1 2 3 4 5 > id1 with 5qty
2 1 2 3 > id2 with 3qty
3 1 2 > id3 with 2qty
IDCode2=2 3 4 > follow id from ID
IDCode2Qty=3 3 1
IDCode2Loc=4 5 6 3 4 5 1 > data is correct
2 4 5 6 > id2 with 3qty continue follow idcode1loc
3 3 4 5 > id3 with 3qty continue follow idcode1loc
4 1 > id4 with 1qty continue follow idcode1loc
IDCode3=1 3 4 > follow id from ID
IDCode3Qty=1 1 5
IDCode3Loc=6 6 2 3 4 5 6 > data is correct
1 6 > id1 with 1qty continue follow idcode2loc
3 6 > id3 with 1qty continue follow idcode2loc
4 2 3 4 5 6 > id4 with 5qty continue follow idcode2loc
And this is my shell script scriptdemo.sh
#!/bin/bash
cd /home/usertest/test/script
logfile="/home/usertest/test/log/"$MYNAME_NOEXT.`date +"%Y%m%d"`.log #create log file
(
RESULT_FOLDER=/home/usertest/test/result
FILE_FOLDER=/home/usertest/test/files
cd $FILE_FOLDER
ls "$FILE_FOLDER" | while read tempfile #loop all files in folder
do
file="$tempfile"
echo >> "$file"
IFS=" " read -ra id_arr <<< "$(grep "ID" "$file" | cut -d "=" -f 2)" #get id insert to array
IFS=" " read -ra idcode1_arr <<< "$(grep "IDCode1" "$file" | cut -d "=" -f 2)" #get idcode1 insert to array
IFS=" " read -ra idcode1qty_arr <<< "$(grep "IDCode1Qty" "$file" | cut -d "=" -f 2)" #get idcode1qty insert to array
IFS=" " read -ra idcode2_arr <<< "$(grep "IDCode2" "$file" | cut -d "=" -f 2)" #get idcode2 insert to array
IFS=" " read -ra idcode2qty_arr <<< "$(grep "IDCode2Qty" "$file" | cut -d "=" -f 2)" #get idcode2qty insert to array
IFS=" " read -ra idcode3_arr <<< "$(grep "IDCode3" "$file" | cut -d "=" -f 2)" #get idcode3 insert to array
IFS=" " read -ra idcode3qty_arr <<< "$(grep "IDCode3Qty" "$file" | cut -d "=" -f 2)" #get idcode3qty insert to array
#array with right idcodeloc
newidcode1loc=""
newidcode2loc=""
newidcode3loc=""
#code to resolve or algorithm method
#for, loop, do while....etc
#replace oldcodeloc with newcodeloc in file
sed -i "s/^IDCode1Loc=.*/IDCode1Loc=$newidcode1loc/" "$file"
sed -i "s/^IDCode2Loc=.*/IDCode2Loc=$newidcode2loc/" "$file"
sed -i "s/^IDCode3Loc=.*/IDCode3Loc=$newidcode3loc/" "$file"
touch -r ../dummy_file "$tempfile" #create dummy file
mv "$tempfile" "$RESULT_FOLDER" #move file to result folder
done
) >> $logfile 2>&1
Thanks all for review.