Search code examples
bashsedminix

sed: Replace a whole line by number overwriting the original file


I am writinig a BASH shell script on MINIX where I want to replace entirely a file's specific line by its number with a certain string, and this replacement to be overwriten to the original file. Not printed on the stdout.

The number of the line and the replacement string are variables of the script with the following names:

line (e.g. 8)
repstring (e.g. "134|Wan|Cho|1988-03-20")

So I m trying the following code:

sed -i "${line}s/.*/${repstring}/" $filename

But when I try to execute the script I get the message:

sed: unknown option -- i
usage:  sed [-aEnr] script [file ...]
        sed [-aEnr] [-e script] ... [-f script_file] ... [file ...]

Is it possible to make this file editing in another way?

p.s. I am aware of redirecting the output of the sed to a new file and then move the new file to the original, but I don't want to create any other files during the process.


Solution

  • According to the documentation, MINIX uses a very minimal version of sed. If you want to stick with sed, just use redirection, and then move the output file to replace the old one.

    sed "${line}s/.*/${repstring}/" "$filename" > /tmp/sed.tmp && mv /tmp/sed.tmp "$filename"
    

    Or, use Perl:

    perl -pi -e "if (\$.==$line) {s/.*/$repstring/ && end}" "$filename"
    

    The variable $. represents the current line number.