简体   繁体   中英

how to do change wit sed in bash script

Hi below is my bash script. which takes a source file and a token file, token file contains servicename:usage I have to find servicename in source file line by line if found then calculate memory usage then change -Xmxm with -Xmx\\d{1,3}m. In below script bold line explain what to do much simple You can first under stand issue from below small part of script

So what is the wrong in above line

#!/bin/bash
sourceFile=$1
tokenFile=$2
if [ -z $sourceFile ]
then
echo "Please provide a valid source file"
exit 0
fi
if [ -z $tokenFile ]
then
echo "Please provide a valid token file"
exit 0
fi
#read token file and tokenize with : to get service name at 0 index and percentage usages at 1
declare arr_token_name
declare arr_token_usage
count=0
while read line
do
#here line contain :percentage usages
OIFS="$IFS"
IFS=$':'
arr=($line)
IFS="$OIFS"
if [ ! -z $line ]
then
arr_token_name[$count]=${arr[0]}
arr_token_usage[$count]=${arr[1]}
count=`expr $count + 1`
fi
done # read source file line by line test with all the tokens
totalMemKB=$(awk '/MemTotal:/ { print $2 }' /proc/meminfo)
echo "total mem = $totalMemKB"
while read line
do
result_token_search=""
#for j in "${arr_token_name[@]}"
#do
# echo "index=$j"
#done
count2=0
for i in "${arr_token_name[@]}"
do
#here search token in line , if found
#calculate memory for this getting percent usage from arr_token_usage then use calculate frmula then device by 1024
#then replace -Xmx\d{1,5}m with -Xmx
echo "line1=$line"
result_token_search=$(echo $line|grep -P "$i")
if [ -n "$result_token_search" ]
then
percent_usage=${arr_token_usage[$count2]}
let heapKB=$totalMemKB*$percent_usage/100
let heapMB=$heapKB/1024
echo "before sed=$line"

echo "new line=$line"
echo "token found in line $line , token = $i"
fi
result_token_search=""
count2=`expr $count2+1`
cat "$line" >> tmp.txt
done
done

try this line:

line=$( sed  "s/-Xmx[0-9]\+/-Xmx$heapMB/" <<<$line )

test with your example:

kent$  line="Superviser.childOpts:-Xmx128m" 

kent$  heapMB=750    

kent$  line=$( sed  "s/-Xmx[0-9]\+/-Xmx$heapMB/" <<<$line )    

kent$  echo $line
Superviser.childOpts:-Xmx750m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM