>Hi,
>Basicly a simple problem, but more complex in UNIX-Shell programming.
>1. Changes is to be done only to lines ending with XX.
>2. All '1' should be changed to '2', but only if the same position on
>the next line is '1'.
>3. All '13' should be changes to '99', but only if the same positions
>on the next line is ' 1'.
>I have to solve the problem using standard UNIX, so perl is not an
>option.
Well, this was a good chance to brush up on my awk programming. I
have a solution that I think works in most cases. I never found a
case it didn't work. Here is MY solution; I'm sure there are many.
BEGIN { bUseLine=0 }
#-------------------------------------
# createMask
#-------------------------------------
function createMask(prev, curr) {
msk=""
is13=0
for (i=1; i<=length(prev); i++) {
if (substr(prev,i,2) == "13")
if (substr(curr,i,2) == "11") {
is13=1
msk=msk "9"
} else if (substr(curr,i,1) == "1")
msk=msk "2"
else
msk=msk " "
else if (substr(prev,i,1) == "1")
if (substr(curr,i,1) == "1")
msk=msk "2"
else
msk=msk " "
else if (substr(prev,i,1) == "3" && is13) {
msk=msk "9"
is13=0
} else
msk=msk " "
}
return msk
Quote:}
#-------------------------------------
# applyMask
#-------------------------------------
function applyMask(prev, msk) {
outStr=""
for(i=1; i<=length(prev); i++) {
mskChar = substr(msk,i,1)
if (mskChar == " ")
outStr = outStr substr(prev,i,1)
else
outStr = outStr mskChar
}
return outStr
Quote:}
#-------------------------------------
# Main body
#-------------------------------------
{
if (bUseLine) {
bUseLine=0
mask = createMask(prevLine, $0)
print applyMask(prevLine, mask)
} else
print prevLine
if ($0 ~ /XX$/)
bUseLine=1
prevLine = $0
Quote:}
END {print prevLine}
--
Scott Erwin
"It never hurts to help!" -Eek the Cat