As a one-liner to demonstrate the concept : echo -e 'a\nb\nc\nPATTERN\nd\ne\nf' | sed -r '1,/PATTERN/ s/^(.*)$/#&/' You just have to adapt to your context : * as for the 'PATTERN' * I assumed '#' as the commenting character * and regarding how you can apply this to all your files. If they all are 'fileXXX.txt', you can run : `sed -ri '1,/PATTERN/ s/^(.*)$/#&/' file*txt`