Friday 15 June 2012

bash - Linux: how to insert missing text between delimiters in a file -



bash - Linux: how to insert missing text between delimiters in a file -

let's have text file has 1 or more pairs of delimiters surround various directives. each delimiter appears on line itself, follows:

{ directive1 ... } { directive1 ... } ...

note opening delimiter different closing one. using "standard" linux tools (sed, awk, grep, perl, etc), how can go through file , check each pair of delimiters (block) specific directive (say "directivex"): if doesn't exist insert block. if exists, don't alter particular block.

i'd utilize perl such task. next script works trivial cases (no nesting, directive never on same line curly brackets):

#!/usr/bin/perl utilize warnings; utilize strict; ($inside, $found); while (<>) { if (/\{/) { $inside = 1; $found = 0; } elsif (/\}/) { print "directive\n" unless $found; $inside = $found = 0; } elsif (/directive/ , $inside) { $found = 1; } print; }

bash

No comments:

Post a Comment