Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have log files coming from other servers, the content in file something wrong and I want to rewrite content before move file. I'm not familiar with shell script in linux, but I tried research and learn around. So, I have idea about, but so hard to tranfers to script. If you have any idea or knowledge about this, can give some advices in this case, or even script. For example, my files with contents

Name=John
IDArray=1 2 3 4
Age=32
IDArray1=1 2 3
IDArray1Qty=5 3 2
IDArray1Location=1 3 2 4 5 1 3 2 2 1
IDArray2=2 3 4
IDArray2Qty=3 3 1
IDArray2Location=4 6 5 3 5 4 2
IDArray3=1 3 4
IDArray3Qty=1 1 5
IDArray3Location=6 4 2 3 4 6 5


The lines IDArray1Location,IDArray2Location, IDArray3Location is wrong. The right values must like this.

Name=John
IDArray=1 2 3 4
Age=32
IDArray1=1 2 3
IDArray1Qty=5 3 2
IDArray1Location=1 2 3 4 5 1 2 3 1 2
IDArray2=2 3 4
IDArray2Qty=3 3 1
IDArray2Location=4 5 6 3 4 5 1
IDArray3=1 3 4
IDArray3Qty=1 1 5
IDArray3Location=6 6 2 3 4 5 6


Noted: I will explain how to calculator the number of line IDArray1Location,IDArray2Location, IDArray3Location. They being calculated based on IDArray and IDArrayQty and have relationship of them. The values of IDArray1,IDArray2,IDArray3 must be in IDArray {1,2,3,4} and the maximum value of each ID is 6.

#IDArray1Location: The IDArray1 have 3 values {1,2,3} and IDArray1Qty have corresponding 3 values {5,3,2} also.
ID1 with 5qty : 1 2 3 4 5 , ID1 not exist, the values will begin 1 2 3 4 5
ID2 with 3qty : 1 2 3 , ID2 not exist, the values will begin 1 2 3
ID3 with 2qty : 1 2 , ID3 not exist, the values will begin 1 2
IDArray1Location=1 2 3 4 5 1 2 3 1 2

#IDArray2Location: The IDArray2 have 3 values {2,3,4} and IDArray2Qty have corresponding 3 values {3,3,1} also. Hence, the values continue from IDArray1Location
ID2 with 3qty : 4 5 6 , ID2 already with values 1 2 3 in IDArray1Location, the values will continue 4 5 6
ID3 with 3qty : 3 4 5 , ID3 already with values 1 2 in IDArray1Location, the values will continue 3 4 5
ID4 with 1qty : 1 , ID4 not exist in IDArray1Location, values begin with 1
IDArray2Location=4 5 6 3 4 5 1

#IDArray3Location: The IDArray3 have 3 values {1,3,4} and IDArray3Qty have corresponding 3 values {1,1,5} also. Hence, the values continue from IDArray1Location and IDArray2Location
ID1 with 1qty : 6 , ID1 already with values 1 2 3 4 5 in IDArray1Location, the values will continue 6
ID3 with 1qty : 6 , ID3 already with values 1 2 3 4 5 in IDArray1Location and IDArray2Location, the values will continue 6
ID4 with 5qty : 2 3 4 5 6 , ID4 already with values 1 in IDArray2Location, the values will continue 2 3 4 5 6
IDArray3Location=6 6 2 3 4 5 6

What I have tried:

I have found around and tried with this pseucode. But don't know how to implement the way to generate IDArray1Location,IDArray2Location,IDArray3Location. I not sure but seem like this
#Create IDArray with {1,2,3,4}
#Create IDArray1 with {1,2,3}
#Create IDArray1Qty with {5,3,2}
#Create IDArray2 with {2,3,4}
#Create IDArray2Qty with {3,3,1}
#Create IDArray3 with {1,3,4}
#Create IDArray3Qty with {1,1,5}

#Loop i=0 to IDArray
#	Loop j=0 to IDArray1 
#		If IDArray[i] = IDArray1[j]
#			Loop k=0 to IDArray1Qty
#				If Index[j] = Index[k] # IDArray1 and IDArray1Qty same element
#						Valuedummy = value[index[k]] # get value of ID by index
#							Loop m=0 to Valuedummy.length
#								value(ID[j])=max(value(ID[j])) + 1
#		         					NewIDArray1Location +=value(ID[j])


I have tried write shell script in linux
Bash
#!/bin/bash
cd /home/usertest/test/script #go to script folder 
logfile="/home/usertest/test/log/"$MYNAME_NOEXT.`date +"%Y%m%d"`.log #create log file
(
RESULT_FOLDER=/home/usertest/test/result #define result folder
FILE_FOLDER=/home/usertest/test/files #define file folder
cd $FILE_FOLDER #go to file folder
ls "$FILE_FOLDER" | while read tempfile #loop all files in folder
do
    file="$tempfile" #pick up one file 
    echo >> "$file"
	IFS=" " read -ra id_arr <<< "$(grep "IDArray" "$file" | cut -d "=" -f 2)" #get IDArray insert to array
	IFS=" " read -ra idcode1_arr <<< "$(grep "IDArray1" "$file" | cut -d "=" -f 2)" #get IDArray1 insert to array
	IFS=" " read -ra idcode1qty_arr <<< "$(grep "IDArray1Qty" "$file" | cut -d "=" -f 2)" #get IDArray1Qty insert to array
	IFS=" " read -ra idcode2_arr <<< "$(grep "IDArray2" "$file" | cut -d "=" -f 2)" #get IDArray2 insert to array
	IFS=" " read -ra idcode2qty_arr <<< "$(grep "IDArray2Qty" "$file" | cut -d "=" -f 2)" #get IDArray2Qty insert to array
	IFS=" " read -ra idcode3_arr <<< "$(grep "IDArray3" "$file" | cut -d "=" -f 2)" #get IDArray3 insert to array
	IFS=" " read -ra idcode3qty_arr <<< "$(grep "IDArray3Qty" "$file" | cut -d "=" -f 2)" #get IDArray3Qty insert to array
	
	#create New IDArrayLocation
	NewIDArray1Location=""  
	NewIDArray2Location=""
	NewIDArray3Location=""

	#How to generate New IDArrayLocation in this block

	#replace Old IDArrayLocation with New IDArrayLocation in file
	sed -i "s/^IDArray1Location=.*/IDArray1Location=$NewIDArray1Location/" "$file"
	sed -i "s/^IDArray2Location=.*/IDArray2Location=$NewIDArray2Location/" "$file"
	sed -i "s/^IDArray3Location=.*/IDArray3Location=$NewIDArray3Location/" "$file"
    touch -r ../dummy_file "$tempfile" #create dummy file
    mv "$tempfile" "$RESULT_FOLDER"  #move file to result folder
done
) >> $logfile 2>&1
Posted
Comments
k5054 5-Aug-23 13:33pm    
This is probably not a good fit for bash or any other shell interpreter. You might find this much easier in python or similar scripting language.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900