Changing the column for a row in a text file and adding another row

Hi,
I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value .
For Eg .
if the input file has a row :
abc xyz lmn 89 lm nk o p

I would like the script to create something like this :
abc xyz lmn 89 lm nk o p
abc xyz lmn 90 lm nk o p

Incrementing column 4th and creating a new row for it .

awk ' { ++$4 } print $0 ' inputfile

Now this will increment the column but I dont know how to create a new row without disturbing the original row .
Infact I would like to get 10 rows like that , with 4th column of each row getting incremented . I guess once I know how it can be done for 1 row, i can put that in for loop .

Any pointers or help would be appreciated .

$ more process.awk
BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
   $0 = lastrow
   ++$4
   print $0
   lastrow = $0
   ++c
   }
}
$ more tmp/foofile
abc xyz lmn 89 lm nk o p
$ awk -f process.awk tmp/foofile
abc xyz lmn 89 lm nk o p
abc xyz lmn 90 lm nk o p
abc xyz lmn 91 lm nk o p
abc xyz lmn 92 lm nk o p
abc xyz lmn 93 lm nk o p
abc xyz lmn 94 lm nk o p
abc xyz lmn 95 lm nk o p
abc xyz lmn 96 lm nk o p
abc xyz lmn 97 lm nk o p
abc xyz lmn 98 lm nk o p
abc xyz lmn 99 lm nk o p

Cheers
ZB

Thanks Zazzybob . I appreciate your response . I tried your script but I am getting this error .

abc xyz lmn 89 lm nk o p
awk: can't set $0
record number 1

Do you know what could cause this ?

Actually I got it . I had to use nawk . Thanks again for response . I appreciate it .

Hi,
I was trying this script today . If I had to add another column and add 24 instead of 1 on that column and add new row after that , how can I do it .
For example if the input line is :
abc xyz lmn 89 lm nk o p 2 p

and I want

abc xyz lmn 89 lm nk o p 2 p
abc xyz lmn 90 lm nk o p 26 p
abc xyz lmn 91 lm nk o p 50 p
.
.
.
.
upto 10 lines .

I tried adding 24 to the $9 in the script above , like this .

BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
$0 = lastrow
++$4
$9 = $9 + 24
print $0
lastrow = $0
++c
}
}

But the result says :
abc xyz lmn 89 lm nk o p 2 p

1 24
1 24 1 24
1 24 1 25 24
1 24 1 26 24 24
1 24 1 27 24 24 24
1 24 1 28 24 24 24 24
1 24 1 29 24 24 24 24 24
1 24 1 30 24 24 24 24 48
1 24 1 31 24 24 24 24 72
1 24 1 32 24 24 24 24 96

I think I am messing up the addition in the column 9 .
Whats the correct way to do it ?
Any help would be appreciated .

Thanks.

It worked for me:

$ more foofile
abc xyz lmn 89 lm nk o p 2 p
$ more process.awk
BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
$0 = lastrow
++$4
$9 = $9 + 24 
print $0
lastrow = $0
++c
}
}
$ awk -f process.awk foofile 
abc xyz lmn 89 lm nk o p 2 p
abc xyz lmn 90 lm nk o p 26 p
abc xyz lmn 91 lm nk o p 50 p
abc xyz lmn 92 lm nk o p 74 p
abc xyz lmn 93 lm nk o p 98 p
abc xyz lmn 94 lm nk o p 122 p
abc xyz lmn 95 lm nk o p 146 p
abc xyz lmn 96 lm nk o p 170 p
abc xyz lmn 97 lm nk o p 194 p
abc xyz lmn 98 lm nk o p 218 p
abc xyz lmn 99 lm nk o p 242 p

I wonder what is the significant difference between our systems.

Ok, I got a similar (but not exactly identical) result by leaving out "{ print $0; lastrow = $0 }". Are you sure there isn't a typo somewhere? Try copying the code directly from your message, because that code works fine.

Thanks for trying . But I dont know why I am not getting the same result as you are .
My system info is :
uname -a
SunOS qa-smart-mgts 5.8 Generic_117350-04 sun4u sparc SUNW,Ultra-80.

I run the same code . I run it as nawk.
nawk -f process.awk filename.
and get
abc xyz lmn 89 lm nk o p 2 p

1 24
1 24 1 24
1 24 1 25 24
1 24 1 26 24 24
1 24 1 27 24 24 24
1 24 1 28 24 24 24 24
1 24 1 29 24 24 24 24 24
1 24 1 30 24 24 24 24 48
1 24 1 31 24 24 24 24 72
1 24 1 32 24 24 24 24 96

I have no idea why.

Ok. I missed the fact that the original line prints followed a blank line. Sorry about that. It looks like value for "lastrow" is not carried over into the END block. Which is weird because the first version worked fine. Are you positive there isn't a typo somewhere? There's not much else different between the two versions.

$ more foofile
abc xyz lmn 89 lm nk o p 2 p
ewalkev@brossrc> more process.awk
BEGIN { c = 0 }

{ print $0; lastrow = $0 }

END { while ( c < 10 ) {
$0 = lastrow
++$4
$9 = $9 + 24 
print $0
lastrow = $0
++c
}
}
$ uname -a
SunOS some_host 5.8 Generic_117350-05 sun4u sparc SUNW,Sun-Fire-880
$ nawk -f process.awk foofile
abc xyz lmn 89 lm nk o p 2 p
abc xyz lmn 90 lm nk o p 26 p
abc xyz lmn 91 lm nk o p 50 p
abc xyz lmn 92 lm nk o p 74 p
abc xyz lmn 93 lm nk o p 98 p
abc xyz lmn 94 lm nk o p 122 p
abc xyz lmn 95 lm nk o p 146 p
abc xyz lmn 96 lm nk o p 170 p
abc xyz lmn 97 lm nk o p 194 p
abc xyz lmn 98 lm nk o p 218 p
abc xyz lmn 99 lm nk o p 242 p

As you can see, Solaris 8, and it's fine....

Cheers
ZB