Re: inserting beg/end lines by blocks of text



Hi Seb, hello netlanders,

On Fri, 23 Nov 2007 03:30:48 +0000, Seb wrote:

<snip>
Based on input such as:

---<---------------cut here---------------start-------------->--- 1
test1
1 test2
1 test3
2 test1
2 test2
2 test3
2 test4
3 test1
3 test2
3 test3
---<---------------cut here---------------end---------------->---

I'm trying to produce output:

---<---------------cut here---------------start-------------->--- 1 beg
1 test1
1 test2
1 test3
1 end
2 beg
2 test1
2 test2
2 test3
2 test4
2 end
3 beg
3 test1
3 test2
3 test3
3 end
---<---------------cut here---------------end---------------->---

which I'm able to obtain with:

awk '{
old=new
new=$1
}
$1 != old {
if (oline) print oline
newl=$0
$2="beg"
print $0
print newl
old=$1
next
}
$1 == old { print; $2="end"; oline=$0 } END {print oline}
'

but it seems somewhat contrived, I wonder if there's a better way to do
it. Any comments appreciated.
<snip>

a less contrived script is:

$1 != prev { pre() }
$1 != prev { print $1, "beg"; prev = $1 }
1
END { pre() }

function pre() {
if (prev) print prev, "end"
}

It handles also the case of empty input correctly.

Hope I could help you,

Steffen "goedel" Schuler
.