Search code examples
sedsyntax

Using sed to read a file. Why do I get `sed: -e expression #1, char 11: missing filename in r/R/w/W commands`?


I have a file with a line starting with Version:. I was trying to detect such line content from the shell and finally I used

grep 'Version:' filename

But before, I was trying to use sed without success with the read command.

sed '/Version:/r' filename
sed: -e expression #1, char 11: missing filename in r/R/w/W commands

Why do I get this error message? What did I do wrong?


Solution

  • The syntax of the r command requires an argument. This is probably not at all what you are trying to do, but the command

    sed '/Version/r /tmp/foo' filename
    

    would insert the contents of /tmp/foo immediately before any line matching /Version/ in filename, and output the results to standard output.

    In other words, sed already knows how to read an input file, just like grep; the r command allows you to pull in the contents of an additional file.

    To simply print the matching lines (and no others) the sed syntax would be

    sed -n '/Version/p' filename
    

    where the -n option disables the default printing of every line, and the p command says to print the line which matched the condition.