I am trying to prompt the user for two pieces of information:
1) The line number in the file to be changed 2) The value to change it to.
My script so far:
echo "Do you wish to enter a variable to be changed?"
select yn in "Yes" "No"; do
case $yn in
Yes)
echo "Please enter a variable to be changed"
read lineNum
echo "Please enter variable's new value"
read val
awk -v lineNum="$lineNum" -v val="$val" \
'BEGIN { if (NR == lineNum) print val; else print $0}' \
test.in > newtest.in
;;
No)
break;;
esac
done
The test.in has numeric values all in one column:
1
2
3
4
5
6
7
Thanks.
EDIT Forgot to mention the error :D. For some reason, the a new file is created but only with the new val that was specified, I imagine this might have something to do with using $0, but I haven't been able to figure out how to fix it.
BEGINforawkthere, and change if statement toif (NR == lineNum) {print val;} else {print $0}, or betterprint (NR==lineNum)?val:$0read -p "prompt: " varnameinstead ofecho "prompt"; read varname; forselect, usePS3="select prompt: "