Print a field from the previous line

plz help me!!
I have this file ,

3408   5600
3796   6035
4200   6285
4676      0
  40   1554
 200   1998
 652   2451
 864   2728
1200      0

I want it like if $2==0,replace it with field from the previous line+500
say here the o/p would be like

3408   5600
3796   6035
4200   6285
4676   6785
  40   1554
 200   1998
 652   2451
 864   2728
1200   3228

thanks in advance

awk '$2{x=$2+500}!$2{$2=x}1' infile

or ... if on SunOS/Solaris

nawk '$2{x=$2+500}!$2{$2=x}1' infile
# cat tst
3408 5600
3796 6035
4200 6285
4676 0
40 1554
200 1998
652 2451
864 2728
1200 0
# nawk '$2{x=$2+500}!$2{$2=x}1' tst
3408 5600
3796 6035
4200 6285
4676 6785
40 1554
200 1998
652 2451
864 2728
1200 3228

sorry its not working and giving me the same output.

---------- Post updated at 08:06 AM ---------- Previous update was at 08:04 AM ----------

I am restriceted in using nawk,but the

awk '$2{x=$2}!$2{$2=x}1' infile

.

is not working for me

---------- Post updated at 08:06 AM ---------- Previous update was at 08:06 AM ----------

I am restriceted in using nawk,but the

awk '$2{x=$2}!$2{$2=x}1' infile

is not working for me

what is your OS ?
did you try with awk nawk and other mawk gawk ?

or also try this if on a SunOS /Solaris plateform

/usr/xpg4/bin/awk '$2{x=$2+500}!$2{$2=x}1' yourfile

??

Well, its Linux.
The o/p I am looking for is

3408 5600
3796 6035
4200 6285
4676 6785
40 1554
200 1998
652 2451
864 2728
1200 3228

NOT

3796 6035
4200 6285
4676 6285
40 1554
200 1998
652 2451
864 2728
1200 2728

Cheers

Ah ... ooooops ok i missed the +500 ... let me fix my previous posts ...

... ok fixed

Note that you will have the following constraint :

1) Your first line must not have 0 in the field $2
2) If you have many consecutive line with the value 0 (in the column you want to check) they will have the same value (they will not be last $2+500)

if you want it to behave so then :

awk '$2{x=$2}!$2{x+=500;$2=x}1' infile

see the difference :

# cat tst
3408 5600
3796 6035
4200 6285
4676 0
4677 0
4678 0
40 1554
200 1998
652 2451
864 2728
1200 0
# awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
3408 5600
3796 6035
4200 6285
4676 6785
4677 7285
4678 7785
40 1554
200 1998
652 2451
864 2728
1200 3228
# awk '$2{x=$2}!$2{$2=x+500}1' tst
3408 5600
3796 6035
4200 6285
4676 6785
4677 6785
4678 6785
40 1554
200 1998
652 2451
864 2728
1200 2728
0      0   3796   6035     151401.65    9486388.65  SY10068003   3444   3083
    0      0   420 6285     151401.65    9486388.65  SY10068003   3444   3083
    0      0   4676 6785     151401.65    9486388.65  SY10068003   3444   3083
    0      0     65   1564     151401.65    9486388.65  SY10068003   3524   3083
    0      0    213   2100     151401.65    9486388.65  SY10068003   3524   3083

---------- Post updated at 08:24 AM ---------- Previous update was at 08:24 AM ----------

0      0   3796   6035     151401.65    9486388.65  SY10068003   3444   3083
    0      0   420 6285     151401.65    9486388.65  SY10068003   3444   3083
    0      0   4676 6785     151401.65    9486388.65  SY10068003   3444   3083
    0      0     65   1564     151401.65    9486388.65  SY10068003   3524   3083
    0      0    213   2100     151401.65    9486388.65  SY10068003   3524   3083

... Ok so ...... then replace $2 with $4

with the scriptu sent I have o/p

3408   5600
3796   6035
4200   6785.......why this is changing from 6285?...this shud be 6285.
4676   6785
  40   1554
 200   1998
 652   2451

plz help me vt that!

---------- Post updated at 11:20 PM ---------- Previous update was at 11:20 PM ----------

with the scriptu sent I have o/p

3408   5600
3796   6035
4200   6785.......why this is changing from 6285?...this shud be 6285.
4676   6785
  40   1554
 200   1998
 652   2451

plz help me vt that!

---------- Post updated at 11:24 PM ---------- Previous update was at 11:20 PM ----------

I/P
cat tst

3408   5600
3796   6035
4200   6285
4676      0
  40   1554
 200   1998
 652   2451

code:

awk '$2{x=$2}!$2{$2=x+500}1' tst

O/P

3408 6100
3796 6535
4200 6785
4676 6785
40 2054
200 2498
652 2951

so actually all the $2 part is getting incremented by 500,instead it shud be only for if $2==0.
But I wonder why ur O/P looks different from mine with the same code?
Ne1 there to come with a prompt reply?
Appreciated!!!

Hi,

Refer to my post #6.

Otherwise, i have no idea why you have a result different from mine, because i tested my code on a linux machine:

# uname -a
Linux xxxxxx 2.4.21-40.ELsmp #1 SMP Thu Feb 2 22:22:39 EST 2006 i686 i686 i386 GNU/Linux
# cat tst
3408 5600
3796 6035
4200 6285
4676 0
4677 0
4678 0
40 1554
200 1998
652 2451
864 2728
1200 0
# awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
3408 5600
3796 6035
4200 6285
4676 6785
4677 7285
4678 7785
40 1554
200 1998
652 2451
864 2728
1200 3228

Hi,

kilo2[m1] cat tst
3408 5600
3796 6035
4200 6285
4676 0
4677 0
4678 0
40 1554
200 1998
652 2451
864 2728
1200 0
kilo2[m1] awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
awk '$2{x=$2}tst2{x+=500;$2=x}1' tst
3408 5600
3796 6035
4200 6285
4676 0
4677 0
4678 0
40 1554
200 1998
652 2451
864 2728
1200 0

Thats what I am getting as O/P...strange!!!
thanks for ur help,but can u plz help me modifying the script in a alternative way getting rid of 1' (in the script).....
cheers!

it looks as if you entered the command twice

kilo2[m1] awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
awk '$2{x=$2}tst2{x+=500;$2=x}1' tst

instead of just

awk '$2{x=$2}!$2{x+=500;$2=x}1' tst

Whatever command I execute prior to run the script get in the script part,thats bit weird.
say in this case,
kilo2[m1] cd /kill/disco
kilo2[m1] awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
awk 'awk: (FILENAME=tst FNR=1) fatal: division by zero attempted
$2{x=$2}/kill/disco2{x+=500;$2=x}1' tst
kilo2[m1]
Can you please help me
Thanks in advance

---------- Post updated at 05:33 AM ---------- Previous update was at 05:33 AM ----------

Whatever command I execute prior to run the script get in the script part,thats bit weird.
say in this case,
kilo2[m1] cd /kill/disco
kilo2[m1] awk '$2{x=$2}!$2{x+=500;$2=x}1' tst
awk 'awk: (FILENAME=tst FNR=1) fatal: division by zero attempted
$2{x=$2}/kill/disco2{x+=500;$2=x}1' tst
kilo2[m1]
Can you please help me
Thanks in advance

What is the name of your file ?
In which column do you want to replace the 0 ? (the 4th column ? or the 2cnd column ? or other?)

0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083

If $4 == 0 ;replace that value with previous line's ($4value+500).Thats what is needed!
Ur script is quite fine but I dont know about the confusion it has with my system.
PLease avoid 1' in the script.

The O/p shud be like
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
Cheers!

---------- Post updated at 05:44 AM ---------- Previous update was at 05:44 AM ----------

0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083

If $4 == 0 ;replace that value with previous line's ($4value+500).Thats what is needed!
Ur script is quite fine but I dont know about the confusion it has with my system.
PLease avoid 1' in the script.

The O/p shud be like
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
Cheers!

You can also try

awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}1' tst

or

If you want to avoid the 1 you can also give a try to one of those and see if it works as you expect:

awk '$4{x=$4}!$4{x+=500;$4=x}{print}' tst

or

awk '$4{x=$4}!$4{x+=500;$4=x}{print $0}' tst

or

awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}{print}' tst

or

awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}{print $0}' tst

or

awk '$4{x=$4}!$4{x+=500;sub($4,x,$0)}{print}' tst

or

awk '$4{x=$4}!$4{x+=500;sub($4,x,$0)}{print $0}' tst

For me, it works withe the 1 :

# cat tst
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 4677 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 4678 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
0 0 4567 0 151401.65 9486388.65 SY10068003 3524 3083
# awk '$4{x=$4}!$4{x+=500;$4=x}1' tst
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 4677 7285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4678 7785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
0 0 4567 2600 151401.65 9486388.65 SY10068003 3524 3083

Hi,
It looks like whatever script I type on the command line it takes the previous command on the !$ part of the script.That need to be sorted out first.
What cud be the reason?
I am using fedora13
Cheers