Good for you.
But
sed should work as well, if you have the syntax right.
Code:
line=5
sed -i "$line d" infile
sed -i "${line}d" infile
All you have to do is make sure the variable is in double-quotes, so that it will expand, and that there's a syntactical separation between it and the one-letter command following it. This can be a space, or you can use the full form of the variable.
The nice thing about
sed (the
gnu version at least), is that you can use the
-i option to edit the original file in place.
BTW, even your
awk command can be made cleaner:
Code:
awk -v "line=$rec" 'NR != line' data.txt > temp.txt
mv temp.txt data txt
Shell variables should generally be copied into
awk variables via the
-v option.
A few other suggestions too.
1) Instead of a
while loop, try using a
select loop. It's designed especially for doing this kind of thing.
2) You have multiple
read commands all looking at the same input. That's not very safe. I suggest configuring the individual actions as
functions and calling those instead. You may have to supply separate file descriptors for the input too (pointing to /dev/tty).
3) Instead of using
printf, use
read's "
-p" option to supply a prompt.
4) Speaking of which, the proper syntax for
printf has a
format string as the first argument and the data to be printed as the following ones. While it will certainly work the way you did it, as long as you're printing a single string only, it's not really correct. Code and data should be kept separate whenever possible.
Code:
printf '%s: ' 'Price' # or use echo -n
printf '%s\n' 'Edit sales entries' # or just use echo
5) Consider using
nl or "
cat -n" to list the file with prefixed line numbers.