Deleting values in a column based on conditions

Hi I have a difficulty in writing shell script for performing the tasks.

A B C D
12 230 16 259
18 260 23 283
21 291 36 298
41 309 49 420
52 425 57 450
61 456 70 473
72 475 79 486

If the A(row no.2) < C(row no.1) then delete value A(row no.1) and so on...
For eg..A3< C2 so delete A2 and B2 so the output would be:

12 230 16 259
18 260 23 283
41 309 36 298
52 425 49 420
61 456 57 450
72 475 70 473
79 486

---------- Post updated 05-12-13 at 12:00 AM ---------- Previous update was 05-11-13 at 11:56 PM ----------

Correcting the required output.The below values from the deleted coloms must replace the above values(shift up).

12 230 16 259
18 260 23 283
41 309 36 298
52 425 49 420
61 456 57 450
72 475 70 473
79 486

Thanks. :slight_smile:

---------- Post updated at 12:02 AM ---------- Previous update was at 12:00 AM ----------

Please ignore above output.

12 230  16  259
18 260  23  283
41 309  36  298
52 425  49  420
61 456  57  450
72 475  70  473
        79  486

This shell bit allows you to write a new file modified as you wish:

while read a b c d ; do
if (( a < c ))
then
 a='  '
fi
 .
 .
 .
 echo $a $b $c $d
done <input_file >new_file

Thanks for coming back.
This code compares the values in same rows. I need to compare the values from different rows,For eg, need to compare 2nd row of column A with 1st row of column C , then delete A2 and B2 if the condition satisfies(A2<C1) and so on. Also values from the deleted coloms must replace the above values(shift up).
Please let me know if you have correctly understood my requirement :slight_smile:

Please post real data and how you would like to have the output, no just example.
And why :slight_smile:
It would help us to help you if we know what and why...

Do you compare roves with original values or with the shifted?
When to shift? While reading, or after all line is read?

Please use code tags as required by forum rules!

None of the outputs you provide satisfies your written request:

A2 and B2 are 18 260 which do not disappear in either output. Pls rephrase carefully your specification!

Thanks for the suggestions. Re-framing my question.

I have a difficulty in writing shell script for performing the tasks.
I have a file group.dat containing below values.

Let suppose 1st colum be A,2nd be B,3rd be C and 4th colom be D.
Now need to compare the 2nd row of A with 1st row of C
condition is

,ie,

, then delete A1 and B1 and shift the values upwards and the loop continues.
We can see that in file group.dat

also

.

So the output I want is

12 230 16 259
21 263 23 283
41 309 36 298
53 431 49 420
72 475 57 450
       70 473
       79 486

Please let me know if I made my requirement clear. :slight_smile:
Thanks in advance.

I showed how to do logic, expecting you to absorb that and add/update with whatever logic you wanted = ....

awk '{++y} {for(x=1;x<=NF;++x) a[x,y]=$x} END {for(r=1;r<y;++r) if(a[1,r+1]<a[3,r]) for(c=1;c<=2;++c) for(r1=r+1;r1<=y;++r1) a[c,r1]=a[c,r1+1]; for(r=1;r<=y;++r) {for(c=1;c<=NF;++c) printf "%-4s",a[c,r]; print ""}}' file

---------- Post updated at 03:12 PM ---------- Previous update was at 02:57 PM ----------

For your last criterion,
the processing order must be backwards:

awk '{++y} {for(x=1;x<=NF;++x) a[x,y]=$x} END {for(r=y-1;r>=1;--r) if(a[1,r+1]<a[3,r]) for(c=1;c<=2;++c) for(r1=r+1;r1<=y;++r1) a[c,r1]=a[c,r1+1]; for(r=1;r<=y;++r) {for(c=1;c<=NF;++c) printf "%-4s",a[c,r]; print ""}}' file

Here is a more "readable" version:

awk '
function up(c,r) {for(;r<=y;++r) a[c,r]=a[c,r+1]}
{++y} {for(x=1;x<=NF;++x) a[x,y]=$x}
END {
for(r=y;r>=2;--r) if(a[1,r]<a[3,r-1]) {up(1,r); up(2,r)}
for(r=1;r<=y;++r) {for(c=1;c<=NF;++c) printf "%-4s",a[c,r]; print ""}
}' file

Of course, by 'column' you mean 'cell' and by 'delete' you mean 'blank out' or 'set to spaces'.