awksedshposixed

How to delete an specific line of a file using POSIX shell scripts?


I want do delete, say line number three of the file a. How can I do this with tools like sed or awk in a way that it's compatible with the POSIX specification? I know I could do sed -i '3d' a, but the -i flag is not specified by POSIX for sed.

Of course I could run the same sed command without the -i flag, save to a temporary file and then replace the original file with the new one; but I would prefer a one-liner.


Solution

  • I am unsure about POSIX compliance, but can suggest using ed to delete the third line of a file as follows:

    echo $'3d\nwq' | ed -s YourFile
    

    or, maybe

    printf "3d\nwq\n" | ed -s YourFile