How to delete already existing data in a file using perl? Pls help me!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete already existing data in a file using perl? Pls help me!!
# 1  
Old 05-05-2010
Bug How to delete already existing data in a file using perl? Pls help me!!

Dear Friends,
I need urgent help from u..
I have two files,file1 & file 2..
file1 have a existing data of file2.So i want to delete those existing datas from file1 (which contain the data from file1)

My file1 like this

rs39348 1 1045729 A G 0.1791 0.2054 0.84
rs37377 1 1061338 A G 0.2845 0.2809 1.01
rs66877 1 1070488 A G 0.1768 0.1956 0.88
rs96512 1 1071463 A G 0.2743 0.2689 1.02
rs49704 1 1088878 G A 0.0922 0.0757 1.24
rs12726 1 1089873 G A 0.1369 0.1231 1.13
rs22982 1 1104902 A G 0.1448 0.1542 0.928

My file2 have a data like
rs37377 1 1061338 A G 0.2845 0.2809 1.018
rs96512 1 1071463 A G 0.2743 0.2689 1.02
rs12726 1 1089873 G A 0.1369 0.1231 1.13
rs22982 1 1104902 A G 0.1448 0.1542 0.928

output should be like following format,
rs39348 1 1045729 A G 0.1791 0.2054 0.84
rs66877 1 1070488 A G 0.1768 0.1956 0.88
rs49704 1 1088878 G A 0.0922 0.0757 1.24

The output should contain non-existing data in a file2..
pls help me friends,,I need a program in perl or R language..I need very urgent

Thanks
Suresh

Last edited by sureshraj; 05-05-2010 at 05:32 AM..
# 2  
Old 05-05-2010
Your explanation and the input/output does not matches.

A program according to your explanation, if need be change it as required.



Code:
open FILE1, '<file1';
open FILE2, '<file2';

# Read the file1 lines, and make it as hash keys.
while ( <FILE1> )  {
        $hash{$_}++;
}


# Read the file2 lines, and if that already exist in file1 ignore that.
while ( <FILE2> )  {
        if ( exists $hash{$_} )  {
                next; 
        }  else  {
                print;
        }
}


Last edited by thegeek; 05-05-2010 at 06:12 AM.. Reason: placed the open commands into the program.
# 3  
Old 05-05-2010
Hi,
try this,

Code:
search=`awk '{print $1}' file2 | paste -s -d'|'`
egrep -v $search file1

# 4  
Old 05-05-2010
u can simply compare both the files using comm command(comm -23 file1 file2). it gives o/p data which is present in 1st file but not in 2nd and redirect the o/p into a new file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

2. Shell Programming and Scripting

Recoding data in a matrix from an existing file

Hi, I was wondering if someone would be able to help with extrapolating information from a file and filling an existing matrix with that information. I have made a matrix like this (file 1): A B C D 1 2 3 4 I have another file with data like this (file 2): 1 A 1 C 3 C 4 B... (1 Reply)
Discussion started by: hubleo
1 Replies

3. Shell Programming and Scripting

Perl - Append data to existing excel cell

Hello All, I have the following code in PERL to write data to excel sheet. Can someone please help me about how to append data to an exisitng cell? For ex in the below given case,Cell 1,1 has Active State PERL Now I want to add a new line like "prorgamming" without overwritting the... (3 Replies)
Discussion started by: prasperl
3 Replies

4. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

5. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

6. Shell Programming and Scripting

add more data to existing data in a file

Hi all, I need help to add additional data from file2 to existing data in file 1 using awk, sed or perl. the ID in file 1 should match against field $3 in file2 file1 #this is a new game ID HR_1 BASE1 30 BASE2 37 DETAIL No TYPE L @@ ID HR_10 BASE1 6030 BASE2 ... (4 Replies)
Discussion started by: redse171
4 Replies

7. UNIX for Dummies Questions & Answers

Add line with data to existing file

i have a file called motors with 3 columns separated with tabs eg: car make reg i want to create a executable file that will add a line with data eg: car make reg benz s600 t35778 can you please show me how to do this? (7 Replies)
Discussion started by: fletcher
7 Replies

8. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies

9. Shell Programming and Scripting

i want to delete a file based on existing file in a directory

hi i am having four files in a directory.like 1)sampleRej 2)exampleRej 3)samplemain 4)examplemain my requirement is i have to search for the rejected files (sampleRej,exampleRej) in a directory.if these files in that directory then i have to delete the main files... (3 Replies)
Discussion started by: srivsn
3 Replies

10. UNIX for Dummies Questions & Answers

delete a file from an existing Solaris tar

How can I delete a file from an existing Solaris tar file ? (not gtar) (2 Replies)
Discussion started by: avnerht
2 Replies
Login or Register to Ask a Question