- Regarding your statement "so in this case a simple
-i filenamewould be the best option as" - no, the best option is for the user to docat foo | command > filename, notcat foo | command -i filename. - There is no "inplace" editing of stdin, you're just directing stdout to a file and that's what
> filenamealready does. - Your -i option should not have a file name argument, that wouldn't make sense and would complicate your code. If there's no input file and someone calls your script with -i then report that as an error, just like sed does, e.g. try
seq 3 | sed -i 's/2/4/'and it'll tell yoused: no input files - As written your code would trash your input file if awk encountered a failure. Use:
> "$tmp_file" &&
mv "$tmp_file" "$file"
See the comments you received and then tryinstead of:
> "$tmp_file"
mv "$tmp_file" "$file"
{ if ($0 ~ num) { $col=value; print } else print }={ if ($0 ~ num) { $col=value } print }which can be written idiomatically as just$0 ~ num { $col=value } 1.- You've removed the shift command I had provided again, thereby making the rest of your code harder to write again.
- You should not have a -f file option, any file(s) you want to use as input should just come after the options+arguments being processed in your getopts loop and be addresses by "$@".
- See also the other comments you received.
Try this (untested) as a starting point: