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
One approch using grep:
grep -f file1 file2 file3 | awk -F"[:-]" '{ print $2"\n"$3; }' >tmp
grep -vf tmp file2 >file2_modified
grep -vf tmp file3 >file3_modified
daPeach
November 20, 2009, 3:06am
3
it could be something like this not POSIXly correct 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
Here is a start:
$> grep -Hvf <(grep -hf file1 file2 file3|tr '-' ' '|xargs -n1) file2 file3
file2:B-2
file2:C-2
file3:G-4
aigles
November 20, 2009, 5:12am
5
Solutions using grep aren't bulletproof.
There are problems with the following input files :
File1:
A2
D
File2:
A2-1
B-21
C-21
E-1
F-3
File2:
K-1
D-3
G-4
H-3
I tried a solution using awk :
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
$ cat file2.new
B-21
C-21
$ cat file3.new
G-4
$
Jean-Pierre.
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
rdcwayx
November 22, 2009, 2:50am
7
How about F and H?
$ grep -v -f file1 file2
C-2
B-2
G-5
F-1
H-4
aigles
November 22, 2009, 11:25am
8
awk -F- '
NR==1 { NewFile = FILENAME ".new" }
NR==FNR {
Count++;
File[ Count, "Datas" ] = $0;
File[ Count, "Group" ] = $2;
Group[ $1 ] = $2;
next
}
{
if ($1 in Group) Ignore[Group[$1]]++
}
END {
printf "" > NewFile;
for (i=1; i<=Count; i++) {
if (! (File[i, "Group"] in Ignore) ) {
print File[i, "Datas"] > NewFile;
}
}
}
' file2 file1
Input file 1
A2
D
Input file 2
A2-1
A-9
C-21
B-12
D-4
G-5
F-1
H-4
Output fille
A-9
C-21
B-12
G-5
Jean-Pierre.