Veja uma solução sed que funcionará com qualquer entrada (por exemplo, várias linhas consecutivas correspondentes a pattern ):
sed '1{ # when on first line
x # exchange
s/^/1000/ # replace the empty hold buffer with "1000"
x # exchange back
}
: do # label "do"
/pattern/{ # if the current line matches "pattern"
${ # if we're on the last line
G # append hold buffer content to pattern space
b # go to end of script
}
n # otherwise print and pull in the next line
/^[[:digit:]]/!{ # if this one doesn't start with a digit
x # exchange
p # print (the pattern space is now "1000")
x # exchange back
b do # go to label "do"
}
}' infile
Com gnu sed , pode ser escrito como
sed '1{x;s/^/1000/;x};:b;/pattern/{${G;b};n;/^[[:digit:]]/!{x;p;x;bb}}' infile
Você pode fazer algo semelhante com awk :
awk -vc=0 '!/^[[:digit:]]/{
if (c) {print "1000"}
}
{ if (/pattern/){c=1} else{c=0}
}
END{if (c){print "1000"}
};1' infile
ou seja, defina c=1 nas linhas que correspondem a pattern e c=0 no restante das linhas e em cada linha que não comece com um dígito (assim como no bloco END ) if c está definido (ou 1 - significando que a linha anterior corresponde a pattern ) - se for o caso, imprima 1000 .