PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > scripting
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

scripting

Réponse
 
LinkBack Outils de la discussion
Vieux 27/10/2006, 18h18   #1
mainak.sen@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut scripting


Hi,
I have two files (file 1 has one column and file 2 four columns), I
have to choose the rows of file 2 where column 2 & 3 of file 2 matches
with column 1 of file 1. Anybody has any idea?


To add to this with an example,

file 1 :
1_8
1_9
1_10
1_11


file 2 :
1 1_500 1_600 0.000 1.0 0.0 0.0
1 1_500 1_500 0.000 0.0 0.0 1.0
1 1_9 1_100 0.000 0.50000 0.50000 0.00000
1 1_9 1_200 0.000 0.50000 0.50000 0.00000
1 1_9 1_400 0.000 1.0 0.0 0.0
.....
1 1_8 1_500 2.107 0.59766 0.40234 0.00000
1 1_8 1_9 2.107 0.89431 0.10569 0.00000
1 1_8 1_300 2.107 0.0 1.0 0.0


merge two files such that it will print
1 1_8 1_9 2.107 0.89431 0.10569 0.00000


i.e. the rows of file 2 where col.2 and col.3 matches with any two
entries in file 1

Any would be extremely well appreciated.
Thanks

  Réponse avec citation
Vieux 27/10/2006, 18h43   #2
Dean G.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting


mainak.sen@gmail.com wrote:
> Hi,
> I have two files (file 1 has one column and file 2 four columns), I
> have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> with column 1 of file 1. Anybody has any idea?
>
>
> To add to this with an example,
>
> file 1 :
> 1_8
> 1_9
> 1_10
> 1_11
>
>
> file 2 :
> 1 1_500 1_600 0.000 1.0 0.0 0.0
> 1 1_500 1_500 0.000 0.0 0.0 1.0
> 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_400 0.000 1.0 0.0 0.0
> ....
> 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> 1 1_8 1_300 2.107 0.0 1.0 0.0
>
>
> merge two files such that it will print
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
>
>
> i.e. the rows of file 2 where col.2 and col.3 matches with any two
> entries in file 1
>
> Any would be extremely well appreciated.
> Thanks


Two questions:
1. If 1_8 is in both the second and third col, is that a match, or does
it need two different values from file 1?

2. Is everything space delimited ?

Dean G.

  Réponse avec citation
Vieux 27/10/2006, 18h45   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

mainak.sen@gmail.com wrote:
> Hi,
> I have two files (file 1 has one column and file 2 four columns), I
> have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> with column 1 of file 1. Anybody has any idea?
>
>
> To add to this with an example,
>
> file 1 :
> 1_8
> 1_9
> 1_10
> 1_11
>
>
> file 2 :
> 1 1_500 1_600 0.000 1.0 0.0 0.0
> 1 1_500 1_500 0.000 0.0 0.0 1.0
> 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_400 0.000 1.0 0.0 0.0
> ....
> 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> 1 1_8 1_300 2.107 0.0 1.0 0.0
>
>
> merge two files such that it will print
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
>
>
> i.e. the rows of file 2 where col.2 and col.3 matches with any two
> entries in file 1
>
> Any would be extremely well appreciated.
> Thanks
>


awk 'NR==FNR{arr[$1];next}($2 in arr)&&($3 in arr)' file1 file2

What if col2 and col3 of file2 have the same value and only match with
one entry in file1?

Ed.
  Réponse avec citation
Vieux 27/10/2006, 18h46   #4
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

mainak.sen@gmail.com wrote:
> Hi,
> I have two files (file 1 has one column and file 2 four columns), I
> have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> with column 1 of file 1. Anybody has any idea?
>
>
> To add to this with an example,
>
> file 1 :
> 1_8
> 1_9
> 1_10
> 1_11
>
>
> file 2 :
> 1 1_500 1_600 0.000 1.0 0.0 0.0
> 1 1_500 1_500 0.000 0.0 0.0 1.0
> 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_400 0.000 1.0 0.0 0.0
> ....
> 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> 1 1_8 1_300 2.107 0.0 1.0 0.0
>
>
> merge two files such that it will print
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
>
>
> i.e. the rows of file 2 where col.2 and col.3 matches with any two
> entries in file 1
>
> Any would be extremely well appreciated.
> Thanks
>


First read in data from file 1, then check whether the selected rows of
file 2 are in the stored data set.

awk 'NR==FNR {s[$1]} NR!=FNR && ($2 in s) && ($3 in s)' file1 file2


Janis
  Réponse avec citation
Vieux 27/10/2006, 20h22   #5
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

mainak.sen@gmail.com wrote:
>
> I have two files (file 1 has one column and file 2 four columns), I
> have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> with column 1 of file 1. Anybody has any idea?
>
>
> To add to this with an example,
>
> file 1 :
> 1_8
> 1_9
> 1_10
> 1_11
>
>
> file 2 :
> 1 1_500 1_600 0.000 1.0 0.0 0.0
> 1 1_500 1_500 0.000 0.0 0.0 1.0
> 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> 1 1_9 1_400 0.000 1.0 0.0 0.0
> ....
> 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> 1 1_8 1_300 2.107 0.0 1.0 0.0
>
>
> merge two files such that it will print
> 1 1_8 1_9 2.107 0.89431 0.10569 0.00000


perl -ane'BEGIN { @x{ grep [ chomp ], `cat file1` } = () }
exists $x{ $F[ 1 ] } && exists $x{ $F[ 2 ] } && print' file2



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
  Réponse avec citation
Vieux 27/10/2006, 20h56   #6
amrita.ray@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

Yes, that's a match. Ideally the column 2&3 should have different
numbers, but even if it's same, I can write a one liner awk making them
different: awk '$2!=$3 {print $0}'...
Thanks.



Dean G. wrote:
> mainak.sen@gmail.com wrote:
> > Hi,
> > I have two files (file 1 has one column and file 2 four columns), I
> > have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> > with column 1 of file 1. Anybody has any idea?
> >
> >
> > To add to this with an example,
> >
> > file 1 :
> > 1_8
> > 1_9
> > 1_10
> > 1_11
> >
> >
> > file 2 :
> > 1 1_500 1_600 0.000 1.0 0.0 0.0
> > 1 1_500 1_500 0.000 0.0 0.0 1.0
> > 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_400 0.000 1.0 0.0 0.0
> > ....
> > 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> > 1 1_8 1_300 2.107 0.0 1.0 0.0
> >
> >
> > merge two files such that it will print
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> >
> >
> > i.e. the rows of file 2 where col.2 and col.3 matches with any two
> > entries in file 1
> >
> > Any would be extremely well appreciated.
> > Thanks

>
> Two questions:
> 1. If 1_8 is in both the second and third col, is that a match, or does
> it need two different values from file 1?
>
> 2. Is everything space delimited ?
>
> Dean G.


  Réponse avec citation
Vieux 27/10/2006, 20h58   #7
amrita.ray@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

and yes, everything space delimited.

Dean G. wrote:
> mainak.sen@gmail.com wrote:
> > Hi,
> > I have two files (file 1 has one column and file 2 four columns), I
> > have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> > with column 1 of file 1. Anybody has any idea?
> >
> >
> > To add to this with an example,
> >
> > file 1 :
> > 1_8
> > 1_9
> > 1_10
> > 1_11
> >
> >
> > file 2 :
> > 1 1_500 1_600 0.000 1.0 0.0 0.0
> > 1 1_500 1_500 0.000 0.0 0.0 1.0
> > 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_400 0.000 1.0 0.0 0.0
> > ....
> > 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> > 1 1_8 1_300 2.107 0.0 1.0 0.0
> >
> >
> > merge two files such that it will print
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> >
> >
> > i.e. the rows of file 2 where col.2 and col.3 matches with any two
> > entries in file 1
> >
> > Any would be extremely well appreciated.
> > Thanks

>
> Two questions:
> 1. If 1_8 is in both the second and third col, is that a match, or does
> it need two different values from file 1?
>
> 2. Is everything space delimited ?
>
> Dean G.


  Réponse avec citation
Vieux 27/10/2006, 21h22   #8
amrita.ray@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

Thanks a lot, it works!



Janis Papanagnou wrote:
> mainak.sen@gmail.com wrote:
> > Hi,
> > I have two files (file 1 has one column and file 2 four columns), I
> > have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> > with column 1 of file 1. Anybody has any idea?
> >
> >
> > To add to this with an example,
> >
> > file 1 :
> > 1_8
> > 1_9
> > 1_10
> > 1_11
> >
> >
> > file 2 :
> > 1 1_500 1_600 0.000 1.0 0.0 0.0
> > 1 1_500 1_500 0.000 0.0 0.0 1.0
> > 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_400 0.000 1.0 0.0 0.0
> > ....
> > 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> > 1 1_8 1_300 2.107 0.0 1.0 0.0
> >
> >
> > merge two files such that it will print
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> >
> >
> > i.e. the rows of file 2 where col.2 and col.3 matches with any two
> > entries in file 1
> >
> > Any would be extremely well appreciated.
> > Thanks
> >

>
> First read in data from file 1, then check whether the selected rows of
> file 2 are in the stored data set.
>
> awk 'NR==FNR {s[$1]} NR!=FNR && ($2 in s) && ($3 in s)' file1 file2
>
>
> Janis


  Réponse avec citation
Vieux 27/10/2006, 21h25   #9
amrita.ray@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: scripting

Yes, it works. Thanks.



Ed Morton wrote:
> mainak.sen@gmail.com wrote:
> > Hi,
> > I have two files (file 1 has one column and file 2 four columns), I
> > have to choose the rows of file 2 where column 2 & 3 of file 2 matches
> > with column 1 of file 1. Anybody has any idea?
> >
> >
> > To add to this with an example,
> >
> > file 1 :
> > 1_8
> > 1_9
> > 1_10
> > 1_11
> >
> >
> > file 2 :
> > 1 1_500 1_600 0.000 1.0 0.0 0.0
> > 1 1_500 1_500 0.000 0.0 0.0 1.0
> > 1 1_9 1_100 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_200 0.000 0.50000 0.50000 0.00000
> > 1 1_9 1_400 0.000 1.0 0.0 0.0
> > ....
> > 1 1_8 1_500 2.107 0.59766 0.40234 0.00000
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> > 1 1_8 1_300 2.107 0.0 1.0 0.0
> >
> >
> > merge two files such that it will print
> > 1 1_8 1_9 2.107 0.89431 0.10569 0.00000
> >
> >
> > i.e. the rows of file 2 where col.2 and col.3 matches with any two
> > entries in file 1
> >
> > Any would be extremely well appreciated.
> > Thanks
> >

>
> awk 'NR==FNR{arr[$1];next}($2 in arr)&&($3 in arr)' file1 file2
>
> What if col2 and col3 of file2 have the same value and only match with
> one entry in file1?
>
> Ed.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17971 seconds with 17 queries