Hi,
I am trying to run below script where my input file contains
URL 1:Port1
URL 2:Port2
URL 3:Port3
IFS=" "
#Loop through the file
HSFILE=/home/Temp/Components.txt
for line in $HSFILE ;do
Url=$(awk -F":" '{print $1}' $HSFILE)
Port=$(awk -F":" '{print $2}' $HSFILE)
echo $Url
echo $Port
cat Monitor_Template.hrb | sed -e "s/URL/$Url/g" | sed -e "s/port/$Port/g">> NEWFile_$(date +%F-%T).hrb
sleep 5
done
for single line in the input file this code is working fine but for multiple line in the code it is not working giving me below error
sed: -e expression #1, char 18: unterminated `s' command
sed: -e expression #1, char 11: unterminated `s' command
You want to run it on every line of $HSFILE, but your awk seems to be using the entire file. you may need something like this
for line in $(cat $HSFILE)
do
Url=$(echo $line | awk -F":" '{print $1}')
Port=$(echo $line | awk -F":" '{print $2}')
cat Monitor_Template.hrb | sed -e "s/URL/$Url/g" -e "s/port/$Port/g" >> NEWFile_$(date +%F-%T).hrb
u can simplify it as
while read line ; do
echo $line|awk -F":" '{print $1" "$2}'|read Url Port
sed -e "s/URL/$Url/g" -e "s/port/$Port/g" Monitor_Template.hrb >> NEWFile_$(date +%F-%T).hrb
done < $HSFILE
Thansk Raja and Vidyadhar,
I am getting the ambigous redirection of file error now for
NEWFile_$(date +%F-%T).hrb
I think it is not redirecting the changes in the new file
sed -e "s/URL/$Url/g" -e "s/port/$Port/g" Monitor_Template.hrb >> NEWFile_$(date +%F-%T).hrb
and it is creating 0kb file in result.
For your below code Raja,
for line in $(cat $HSFILE)
do
Url=$(echo $line | awk -F":" '{print $1}')
Port=$(echo $line | awk -F":" '{print $2}')
cat Monitor_Template.hrb | sed -e "s/URL/$Url/g" -e "s/port/$Port/g" >> NEWFile_$(date +%F-%T).hrb
I need each varaible from the input file like URL 1 to replace first and port 1 second.
So my loop should run for three time and create three files after replacing the mentioned values in the input file.
---------- Post updated at 07:04 AM ---------- Previous update was at 06:42 AM ----------
Hi rajamadhavan
I tried your snippet of code
echo $line|awk -F":" '{print $1" "$2}'|read Url Port
if I echo $Url and $Port, It is not showing anything on the console. Value in file is getting replaced by null.