Remove matched values and their related groups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove matched values and their related groups
# 1  
Old 11-20-2009
Remove matched values and their related groups

For each value in file1 it has to check in file2 and file3.
If value matched it has to delete that value and related group value
in file2 and file3.
In this example it takes A , deletes A and take related group value 1 and
deletes E-1,then checks in file3 and deletes K-1.After that it takes D in file1
checks in file2 its not there and then checks in file3 deleted D-3,takes related group
value 3 deletes H-3 and then it should delete F-3 in file2.
file1
------
A
D
file2
--------
A-1
B-2
C-2
E-1
F-3
file3
-----
K-1
D-3
G-4
H-3


O/p needed
--------
file2
-----
B-2
C-2
file3
------
G-4

Need help at the earliest
# 2  
Old 11-20-2009
One approch using grep:

Code:
grep -f file1 file2 file3 | awk -F"[:-]" '{ print $2"\n"$3; }' >tmp
grep -vf tmp file2  >file2_modified
grep -vf tmp file3  >file3_modified

# 3  
Old 11-20-2009
it could be something like this not POSIXly correct code
Code:
#!/bin/bash

searchdelete() {
   group=$(grep $1 $2) && sed -i '/-'"${group#*-}"'$/d' $2 $3
}
while read search; do
   searchdelete $search file{2,3} || searchdelete $search file{3,2}
done < file1


Last edited by daPeach; 11-20-2009 at 06:27 AM.. Reason: add a - in sed sentence to take care of Aigles comment
# 4  
Old 11-20-2009
Here is a start:
Code:
$> grep -Hvf <(grep -hf file1 file2 file3|tr '-' ' '|xargs -n1) file2 file3
file2:B-2
file2:C-2
file3:G-4

# 5  
Old 11-20-2009
Solutions using grep aren't bulletproof.
There are problems with the following input files :
File1:
Code:
A2
D

File2:
Code:
A2-1
B-21
C-21
E-1
F-3

File2:
Code:
K-1
D-3
G-4
H-3

I tried a solution using awk :
Code:
awk -F- '
FNR==1 { 
   if (FileNumber > 0) File[FileNumber, "Count"] = count;
   FileName[++FileNumber] = FILENAME 
   count = 0;
}
FileNumber <= 2 {
   count++;
   File[FileNumber, count, "Datas" ] = $0;
   File[FileNumber, count, "Group" ] = $2;
   Group[$1] = $2;
   next
}
{
   if ($1 in Group) Ignore[Group[$1]]++
}
END {   
   for (f=1; f<=2 ;f++) {
      file = FileName[f] ".new";
      printf "" > file;
      for (i=1; i<=File[f, "Count"]; i++) {
         if (! (File[f, i, "Group"] in Ignore) ) {
            print File[f, i, "Datas"] > file;
         }
      }
   }
}

' file2 file3 file1

The result is files file2.new and file3.new
Code:
$ cat file2.new
B-21
C-21
$ cat file3.new
G-4
$


Jean-Pierre.
# 6  
Old 11-20-2009
thanks for all

---------- Post updated at 06:58 AM ---------- Previous update was at 06:56 AM ----------

Now i need to remove matched values and their related groups in a file only

file1
---
A
D

file2
-----
A-1
C-2
B-2
D-4
G-5
F-1
H-4

o/p ineeded is

file2
-----
C-2
B-2
G-5

wht i have to do
# 7  
Old 11-22-2009
How about F and H?
Code:
$ grep -v -f file1 file2
C-2
B-2
G-5
F-1
H-4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to remove matched rows from my file?

Hello, I am new beginner, and just got help from this forum. The command line is :awk '($1, $2) in x { print x print delete x next } { x = $0 }' results>myfileI got a output "myfile" from the orginal file 'results'. The quesion is I don't know how to get all rows... (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

3. Shell Programming and Scripting

Impute values within groups

Hello all, this is quite complex, so please allow me some space to make the problem clear. I have several groups which has 3 types T1,T2 and T1*T2. The T1 (and T2) values are always in doubles ( aa, cc, gg , tt , dd ,ii ). The T1*T2 values can be either equal to T1 , or equal... (7 Replies)
Discussion started by: jianp83
7 Replies

4. Shell Programming and Scripting

Help related to Script to move files depending on config values

Hi All, I am new to Unix scripting, I have requirement where I need to read the key value pair from config file. Sample Config file: Key(File Pattern) Value(File Directory location) test /Users/Bkumar/Downloads/testdir prod ... (1 Reply)
Discussion started by: sbpkumar7
1 Replies

5. UNIX for Dummies Questions & Answers

Remove groups of repeating lines

I know uniq exists, but am not sure how to remove repeating lines when they are groups of two different lines repeating themselves, without using sort. I need them to be sorted in the original order, just to remove repeats. cd /media/AUDIO/WAVE/9780743518673/mp3 ~/Desktop/mp3-to-m4b... (1 Reply)
Discussion started by: glev2005
1 Replies

6. Shell Programming and Scripting

remove unmatched values

Below is my requirement : unmatched values should get deleted from file1 file1 A-1 B-1 C-1 D-2 E-3 F-4 file2 D C F output C-1 D-2 F-4 (2 Replies)
Discussion started by: lavnayas
2 Replies

7. Shell Programming and Scripting

Get all the values matched in perl

HI, I have sentences like this: @str=("An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells","Elucidation of the mechanism of retinal degeneration of RNA-binding protein","Rna binding protein is an important protein","In the retinal degenerative process... (1 Reply)
Discussion started by: vanitham
1 Replies

8. Shell Programming and Scripting

Need to remove few characters from each line till a pattern is matched

Hi All, I want to remove first few characthers from starting of the line till ',' Comma... which needs to be done for all the lines in the file Eg: File content 1,"1234",emp1,1234 2,"2345",emp2,2345 Expected output is ,"1234",emp1,1234 ,"2345",emp2,2345 How can parse... (4 Replies)
Discussion started by: kiranlalka
4 Replies

9. Programming

kernel values related to mmap

Hi there... I am maitaining an archaic application which is using mmap for file transfering/routing. There are over 500 instances of the application running without any issues for almost 2 decade. Now, the problem is that the on one particular server (HP-UX 11), sometimes the mmap is failing... (4 Replies)
Discussion started by: tobsinte
4 Replies

10. UNIX for Advanced & Expert Users

How to remove UNIX user and groups

I created UNIX groups - oinstall, dba and UNIX user - oracle for the installation of Oracle 10g. But I might did something incorrectly. Oracle user account didn't created properly. How to remove these UNIX groups and user so that I can start over again to create them properly. Thanks. (7 Replies)
Discussion started by: duke0001
7 Replies
Login or Register to Ask a Question