I want to insert some code into multiple tex files foo*.tex in a directory, one line after \documentclass{.*}. Note that the files have different document classes so the .* is just a symbolic placeholder here. The code has multiple lines, for example\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\usepackage{ngerman}What's the best way to do this automatically?
Insert multiline text into several files via commandline
bash;shell script;latex
One way using GNU sed:Content of script.sed:/^\\documentclass[^{]*{[^}]*}/ {    a\\\usepackage[utf8]{inputenc}\\\usepackage[T1]{fontenc}\\\usepackage{ngerman}}Run it like:sed -s -i.bak -f script.sed foo*.texI use ^ to match the documentclass at the beginning of the line. Remove it if you can have spaces before it.The -s switch considers each input as a different file, and -i creates backups with bak extension.