Delete rows with conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete rows with conditions
# 1  
Old 12-03-2014
Delete rows with conditions

Hi everyone,

I will appreciate a lot if anyone can help me about a simple issue.
I have a data file, and I need to remove some rows with a given condition.
So here is a part of my data file:

Code:
5,14,1,3,3,0,0,-0.29977188269E+01
5,16,1,4,4,0,0,0.30394279900E+02
5,13,1,4,4,0,0,0.15108184243E+02
5,-14,1,4,4,0,0,0.38275921713E+01
6,-2,-1,0,0,0,501,0.00000000000E+00
6,2,-1,0,0,501,0,0.00000000000E+00
6,-15,2,1,2,0,0,0.43485959710E+02
6,15,2,1,2,0,0,-0.43485959710E+02
7,4,-1,0,0,501,0,0.00000000000E+00
7,-4,-1,0,0,0,501,0.00000000000E+00
7,23,2,1,2,0,0,0.14210854715E-13
7,-15,2,3,3,0,0,0.57831183575E+02

The first column is the data set ID, I need to remove data sets in which any of its rows contains 23 in the second column. According to given data above, after the operation it should look like:

Code:
5,14,1,3,3,0,0,-0.29977188269E+01
5,16,1,4,4,0,0,0.30394279900E+02
5,13,1,4,4,0,0,0.15108184243E+02
5,-14,1,4,4,0,0,0.38275921713E+01
6,-2,-1,0,0,0,501,0.00000000000E+00
6,2,-1,0,0,501,0,0.00000000000E+00
6,-15,2,1,2,0,0,0.43485959710E+02
6,15,2,1,2,0,0,-0.43485959710E+02

the number 23 in the second column appears only in data set 7, so the entire data set must be removed from the file.

thanks in advance for any help.
# 2  
Old 12-03-2014
I didn't consider too many possible scenarios beyond the sample you gave above; try
Code:
awk     '$1 != L        {if (!D) for (i=1; i<=CNT; i++) print O[i]; delete O; CNT=D=0}
                        {O[++CNT]=$0; L=$1}
         $2==23         {D=1}
         END            {if (!D) for (i=1; i<=CNT; i++) print O[i]}
        ' FS=, file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-03-2014
or alternatively:
Code:
awk -F, 'FNR==NR{if ($2==23) no[$1]; next} !($1 in no)' myfile myfile

These 2 Users Gave Thanks to vgersh99 For This Post:
# 4  
Old 12-03-2014
Hi vgersh99,

your code works quite fine, can you briefly explain your code if you don't mind please?

thanks again.
# 5  
Old 12-03-2014
And the inevitable:
Code:
awk '
  $1!=p {
    if(NR>1 && m) printf "%s", s
    s=x; p=$1; m=1
  }
  { 
    if($2==23) m=0
    s=s $0 ORS
  } 
  END {
    if(m)printf "%s", s
  }
' FS=, OFS=, file


Last edited by Scrutinizer; 12-03-2014 at 09:04 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract duplicate rows with conditions

Gents Can you help please. Input file 5490921425 1 7 1310342 54909214251 5490921425 2 1 1 54909214252 5491120937 1 1 3 54911209371 5491120937 3 1 1 54911209373 5491320785 1 ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Using sed to delete one regular pattern with conditions

Hi All , I am having a file like this INPUT FILE ############################### addfd_mjlala kksks sksks ks annsns_bbox_2 (sksksk ksks ) adnndn_nsns_bbox_3 (( jsjsdj sjsj ) malm_dkdm lsls lsl mdndk_mkmd_dkd_bbox_4_kdkd ksksk skksk_bbox_jsj_KSK ((jsjsj jsjsj )... (2 Replies)
Discussion started by: kshitij
2 Replies

3. Shell Programming and Scripting

Help - delete content inside square brackets under conditions

I have the file sed1.txt and I need to strip the brackets (]) and content inside them only when I have two or three letters followed by a colon. for example,it may be any letter, not just abc ] ] #-- cat sed1.txt 1 ] FISICA 2 ]PORTUGUES 3 ] ]MATEMATICA 4 ]]INGLES ] 5 ]QUIMICA 6... (2 Replies)
Discussion started by: dperboni
2 Replies

4. UNIX for Dummies Questions & Answers

How to delete rows?

I have an Output file which has the result YYYY 95,77 YYYY YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY 95 YYYY YYYY YYYY YYYY I would like to display the above along with a single line with above info. Final output should be YYYY 95 (3 Replies)
Discussion started by: priyanka.premra
3 Replies

5. Shell Programming and Scripting

How to copy or cut specific rows from appended file with some conditions

Hi I have one file which is containing about 5000 rows and 20 columns I will just explain about my requirement here briefly with sample file, I have attached also, please help....me.. 1 28.25 36.42 5 28.26 36.42 10 28.23 36.43 15 28.22 36.43 20 28.2 36.42 25... (6 Replies)
Discussion started by: nex_asp
6 Replies

6. Shell Programming and Scripting

delete the same rows

each individual (row) has genotype expressed for each SNP (column) file1.txt 1 1 A G A T G T A A A A A A A A A A A A A A A A A A A A A 2 2 G A A A A A A A A A A A A A A A A A A A A A A A A A A 3 3 A A A A A A A A A A A A A A A A A A A A A A A A A A A 4 4 G A G T A T A A A A A A A A A A A... (3 Replies)
Discussion started by: johnkim0806
3 Replies

7. Shell Programming and Scripting

Delete duplicate rows

Hi, This is a followup to my earlier post him mno klm 20 76 . + . klm_mango unix_00000001; alp fdc klm 123 456 . + . klm_mango unix_0000103; her tkr klm 415 439 . + . klm_mango unix_00001043; abc tvr klm 20 76 . + . klm_mango unix_00000001; abc def klm 83 84 . + . klm_mango... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

8. Shell Programming and Scripting

delete line with conditions

Hi Dude, I am struck up with another problem,Pls help me I am new to perl , I have little knowledge only. The scenario is ssenthil = rw anilkg = rw @group1 = rw anilkg = rw ssenthil = rw i need output as below , I want to find "anilkg" and delete the preceding... (12 Replies)
Discussion started by: anil8103
12 Replies

9. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

10. Shell Programming and Scripting

How to delete particular rows from a file

Hi I have a file having 1000 rows. Now I would like to remove 10 rows from it. Plz give me the script. Eg: input file like 4 1 4500.0 1 5 1 1.0 30 6 1 1.0 4500 7 1 4.0 730 7 2 500000.0 730 8 1 785460.0 45 8 7 94255.0 30 9 1 31800.0 30 9 4 36000.0 30 10 1 15000.0 30... (5 Replies)
Discussion started by: suresh3566
5 Replies
Login or Register to Ask a Question