regex - How to repeat a block of lines using awk? -
regex - How to repeat a block of lines using awk? -
i'm trying repeat block of lines avobe occurs
word number of times inticated in line. block of lines repeat have smaller number @ start of line.
i mean, input:
01 patient-treatments. 05 patient-name pic x(30). 05 patient-ss-number pic 9(9). 05 number-of-treatments pic 99 comp-3. 05 treatment-history occurs 2. 10 treatment-date occurs 3. 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 10 treating-physician pic x(30). 10 treatment-code pic 99. 05 hello pic x(9). 05 stack occurs 2. 10 overflow pic x(99).
this output:
01 patient-treatments. 05 patient-name pic x(30). 05 patient-ss-number pic 9(9). 05 number-of-treatments pic 99 comp-3. 05 treatment-history occurs 2. 10 treatment-date occurs 3. 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 10 treating-physician pic x(30). 10 treatment-code pic 99. 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 15 treatment-day pic 99. 15 treatment-month pic 99. 15 treatment-year pic 9(4). 10 treating-physician pic x(30). 10 treatment-code pic 99. 05 hello pic x(9). 05 stack occurs 2. 10 overflow pic x(99). 10 overflow pic x(99).
i tried way:
tac input.txt | awk ' begin { lbuff=""; n=0; }{ if($0 ~ /^\s*$/) {next;} if ($3 == "occurs") { lev_oc=$1 len_oc=$4 lstart=0 (x=1; x<n; x++) { split(saved[x],saved_level," ") if (saved_level[1] <= lev_oc) { print saved[x] lstart=x+1 } } (i=1; i<=len_oc; i++) { (x=lstart; x<n; x++) { print saved[x] } } print $0 }else if ($0) { saved[n]=$0 n++ } }' | tac
but don't result i'm trying obtain. awk best way it? have alternative?
i used perl because it's easy create arbitrarily complex info structures:
class="lang-perl prettyprint-override">#!/usr/bin/perl utilize strict; utilize warnings; # read file array of lines. open $f, '<', shift; @lines = <$f>; close $f; @occurring; @occurs; # iterate on lines of file (my $i = 0; $i < @lines; $i++) { # extract "level", first word of line $level = (split ' ', $lines[$i])[0]; # if line contains occurs string, # force info onto stack. # marks start of repeated if ($lines[$i] =~ /occurs (\d+)/) { force @occurring, [$1-1, $level, $i+1]; next; } # if line @ same level level of start of # lastly seen item on stack, mark lastly line of repeated text if (@occurring , $level eq $occurring[-1][1]) { force @occurs, [@{pop @occurring}, $i-1]; } } # if there's open on stack, ends @ lastly line while (@occurring) { force @occurs, [@{pop @occurring}, $#lines]; } # handle lines repeated appending them lastly # line of repetition (@occurs) { $repeated = ""; ($count, undef, $start, $stop) = @$_; $repeated .= bring together "", @lines[$start..$stop] (1..$count); $lines[$stop] .= $repeated; } print @lines;
for reading pleasure, here's awk translation.
class="lang-awk prettyprint-override">begin { s = 0 f = 0 } function stack2frame(lineno) { f++ frame[f,"reps"] = stack[s,"reps"] frame[f,"start"] = stack[s,"start"] frame[f,"stop"] = lineno s-- } { lines[nr] = $0 level = $1 } # if line contains occurs string, force info onto stack. # marks start of repeated $(nf-1) == "occurs" { s++ stack[s,"reps"] = $nf-1 stack[s,"level"] = level stack[s,"start"] = nr+1 next } # if line @ same level level of start of # lastly seen item on stack, mark lastly line of repeated text level == stack[s,"level"] { stack2frame(nr-1) } end { # if there's open on stack, ends @ lastly line while (s) { stack2frame(nr) } # handle lines repeated appending them lastly # line of repetition (i=1; i<=f; i++) { repeated = "" (j=1; j <= frame[i,"reps"]; j++) { (k = frame[i,"start"]; k <= frame[i,"stop"]; k++) { repeated = repeated ors lines[k] } } lines[frame[i,"stop"]] = lines[frame[i,"stop"]] repeated } (i=1; <= nr; i++) print lines[i] }
regex bash awk grep copybook
Comments
Post a Comment