Hello Folks,
I have a file named as date.dat present at /tmp/abc
location which has following data -
20161030,20161031,20161101
I need to remove this line and replace it with something like below -
$param1,$param2,$param3
Param1, Param2 and param3 stores the date based on some calculation in the same script.
Any leads will be greatly appreciated.
First off: what do you mean by "something like"? I am sure you look for a solution, not something like a solution
, no? So, please, tell us what you need, not "something alike".
If i get you correctly you want to replace a line in a file with 3 selectable values, yes?
If so:
#! /bin/ksh
typeset chVal1="$1"
typeset chVal2="$2"
typeset chVal3="$3"
typeset fIn="/tmp/abc/date.dat"
sed '/^20161030,20161031,20161101$/ {
s/20161030/'"$chVal1"'/
s/20161031/'"$chVal2"'/
s/20161101/'"$chVal3"'/
}' "$fIn" > "$fIn".tmp
if [ $? -eq 0 ] ; then
mv "$fIn".tmp "$fIn"
else
print -u2 "Error editing file!
rm -f "$fIn".tmp
exit 1
fi
exit 0
Save this to a file script.sh
, flag it executable and call it with ./script.sh "param1" "param2" "param3"
.
Note that the script is a barebone solution - no effort is made for paramter checking, etc.. Add this functionality ad libitum.
I hope this helps.
bakunin