The following command will add pattern to the fourth line.
sed -i '4i pattern' file
But it is not working if I have less than four lines in the file. The requirement is I need to add the pattern in 4th line with empty prefix lines.
cat -n file
1 line1 2 3 4 pattern
GNU sed version 4.2.1