#! /bin/sh awk '!/input\{/ {print} /input\{/ { sub (/input\{/,"") sub (/\}.*/,"") cmd= "cat ./"$1"" system(cmd) }' inputfile.tex > mergedfile.tex