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
# 1  
Old 05-12-2013
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. Smilie

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

Please ignore above output.
Quote:
it looks like this
Code:
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


Last edited by joeyg; 05-14-2013 at 05:33 PM.. Reason: Corrected spelling in title
# 2  
Old 05-13-2013
This shell bit allows you to write a new file modified as you wish:
Code:
while read a b c d ; do
if (( a < c ))
then
 a='  '
fi
 .
 .
 .
 echo $a $b $c $d
done <input_file >new_file

# 3  
Old 05-14-2013
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 Smilie
# 4  
Old 05-14-2013
Please post real data and how you would like to have the output, no just example.
And why Smilie
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?
# 5  
Old 05-14-2013
Please use code tags as required by forum rules!

None of the outputs you provide satisfies your written request:
Quote:
For eg..A3< C2 so delete A2 and B2 so the output would be:
A2 and B2 are 18 260 which do not disappear in either output. Pls rephrase carefully your specification!
# 6  
Old 05-14-2013
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.
Quote:
12 230 16 259
18 260 23 283
21 263 36 298
41 309 49 420
52 425 57 450
53 431 70 473
72 475 79 486
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
Quote:
(A2<C1)
,ie,
Quote:
18<16
, then delete A1 and B1 and shift the values upwards and the loop continues.
We can see that in file group.dat
Quote:
A3(21) <C2( 23)
also
Quote:
A6(53) <C5( 57)
.

So the output I want is
Code:
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. Smilie
Thanks in advance.

Last edited by Sarwagya Jha; 05-14-2013 at 09:20 AM..
# 7  
Old 05-14-2013
I showed how to do logic, expecting you to absorb that and add/update with whatever logic you wanted = ....
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