Monday, November 15, 2010

Insert a repeated line in a text file with awk

System administrators, DBAs, network gurus, and perhaps developers, often feel the need to insert a particular word or phrase every 'x' number of lines in their script. For example a DBA might have a list of insert statements and needs to place a commit line every ten lines. This would be quite a burden to do it manually for a large script. Worry not! With awk you can easily do it like this:


awk 'ORS=NR%10?"\n":"\ncommit;\n"' db_patch.sql


This example will insert the text commit; every ten lines for the document db_patch.sql. More awk examples will follow on our website.
Post a Comment