Wednesday, December 8, 2021

How to add a string to specific line in a text file

Here is an example of how I inserted a line needed in a postgres pg_restore command, at line 20:
sed -i '20s/^/SET search_path to sales,public;\n/' myfile.txt

If your string contains special characters, remember to escape it properly. In the next example, I want to append the string \timing at line number 7 in myfile.txt:
 sed -i '7s/^/\\timing on\n/' myfile.txt

No comments:

Post a Comment