Friday, November 29, 2013

awk 'BEGIN { cntr = 0 } /repeating string/ { cntr++ ;OFS=""; print "repeating newString", cntr } !/repeating string/ { print $0 }' thisfile.txt>modified-thisfile.txt

This bash script "one-liner" searches for occurrences of "repeating string" in a file and for each occurrence, replaces the string with "repeating newString" and adds a number (cntr) to that new string, so that the output file (modified-thisfile.txt) has "repeating newString1", "repeating newString2", etc., where the original strings were. I used this to modify material assignments in (3D) obj files (in which the original string in question was "usemtl default").

The problem was that I needed to make the materials assigned to each 3D object unique. These obj files were exported from Cheetah3D, and were to be used with the Element3D plugin in After Effects, which uses separate and unique material assignments to separate objects/polygons.

The OFS part ensures that the number is printed directly after the string, instead of, by default, with a space before it.