Deleting values in a column based on conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting values in a column based on conditions
# 8  
Old 05-14-2013
Code:
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:
Code:
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:
Code:
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


Last edited by MadeInGermany; 05-14-2013 at 06:00 PM.. Reason: start row=1
# 9  
Old 05-14-2013
Of course, by 'column' you mean 'cell' and by 'delete' you mean 'blank out' or 'set to spaces'.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

2. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

3. Shell Programming and Scripting

Create new file with increment column based on conditions

Hello, Using bash script, i need to process the following file: 887,86,,2013-11-06,1,10030,5,2,0,200,, 887,86,,2013-11-05,1,10030,5,2,0,199,, 887,138,,2013-11-06,1,10031,6,2,0,1610612736,, 887,164,,2013-11-06,1,10000,0,2,0,36000,, and to create a new file such as the below ... (2 Replies)
Discussion started by: JonhyDeep
2 Replies

4. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

5. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

6. Shell Programming and Scripting

Deleting row if all column values are a particular string

Hello, I have a very large file for which I would like to remove all rows for which the value of columns 2-5 is zero. For instance I would like this file: contig1, 0, 0, 0, 0 contig2, 1, 3, 5, 0 contig3, 0, 0, 0, 0 contig4, 0, 5, 6, 7 To become this file: contig2, 1, 3, 5,0 ... (17 Replies)
Discussion started by: mouchkam
17 Replies

7. Shell Programming and Scripting

Deleting a row based on fetched value of column

Hi, I have a file which consists of two columns but the first one can be varying in length like 123456789 0abcd 123456789 0abcd 4015 0 0abcd 5000 0abcd I want to go through the file reading each line, count the number of characters in the first column and delete... (2 Replies)
Discussion started by: swasid
2 Replies

8. Shell Programming and Scripting

How to averaging column based on first column values

Hello I have file that consist of 2 columns of millions of entries timestamp and throughput I want to find the average (throughput ) for each equal timestamp before change it to proper format e.g : i want to average 2 coloumnd fot all 1308154800 values in column 1 and then print... (4 Replies)
Discussion started by: aadel
4 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question