ntubski |
09-19-2012 09:13 AM |
Quote:
Originally Posted by mrpurple
(Post 4781832)
I'd definitely be happy to hear of something more efficient.
|
In my experience, efficiency in shell scripts is usually comes from minimizing the amount of work done in the shell. In this case, I think awk will work a lot better (so we can do zero work in the shell):
Code:
#!/usr/bin/awk -f
BEGIN {
RS = "\n(\n|$)";
FS = "\n";
block = 0;
}
$1 ~ /\[DATATYPE2\]/ {
for (i = 2; i <= NF-1; i++) {
printf("processing line %d of block %d\n%s\n",
i - 2, block, $i);
}
block++;
}
Basically, RS = "\n(\n|$)" separates each block, and FS = "\n" puts each line of the block in $1, $2, $3, ... $NF (where $1 is the "[DATATYPE]" line and $NF is the "[END]" line.
|