R.Muralikrishnan, MPI for Empirical Aesthetics. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Licence. #!/usr/bin/awk ##################################### # Awk script to replace a certian field on rows that occur between a START and STOP pattern. # These lines are printed with necessary changes, other lines are printed as is. # # Execute as: awk -f Replace-using-start-stop-pattern.awk Input.txt > Ouput.txt # Use the accompanying wrapper shell script for multiple files # # Author: R. Muralikrishnan ##################################### # General Logic: Use text fields/patterns in the file that indicate a starting and ending point # between which changes need to be made. # For lines within these limits, change necessary fields as required, but don't print them yet. # In a second step without any conditional statement, print all lines # (...which will include the changes we made in the previous step). BEGIN{FS="= "; i = 1;} # Input field separator is '= '; # Use the start-stop pattern recognition to treat lines occurring between the patterns. (substr($2,2,3)=="syl"),(substr($2,2,3)=="MAU"){ # The brace has to be here...since we've used the () form # of the 'if' check. Taking it to the next line won't work... # in effect acting as though the check were not at all there. # We are on lines that have name = "syl" and name = "MAU" # Now, if the first field is "text" and # the second one (with FS="= ") begins with a double-quote, # but it is not empty (i.e., not ""), then # change the second field to "C1", "C2", "C3" or "C4" (with the double-quotes) if ($1~/text/ && $2~/\"/ && $2!~/\"\"/) { $2="= \"C"i"\""; # What we want is, in effect = "C1" etc. #print $0; # Don't print the line yet...it will be taken care of in the general section below. i++; } } {print $0}