grep: logical AND across multiple files?

grep: logical AND across multiple files?

Post by john ye » Fri, 23 Jun 2006 03:32:28



I have 8 files with a list of hostnames in each.

Is there a grep one liner that will output just the hostnames common to
_all_ files?

a file example:
hosta
hostb
hostc
hostd

cheers.

 
 
 

grep: logical AND across multiple files?

Post by Xicheng Ji » Fri, 23 Jun 2006 03:45:51



> I have 8 files with a list of hostnames in each.

> Is there a grep one liner that will output just the hostnames common to
> _all_ files?

> a file example:
> hosta
> hostb
> hostc
> hostd

Do you have duplicated hostnames in a single file?? if not, you can try
the following awk line:

    awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

Xicheng

 
 
 

grep: logical AND across multiple files?

Post by Jon LaBadi » Fri, 23 Jun 2006 03:59:09




>> I have 8 files with a list of hostnames in each.

>> Is there a grep one liner that will output just the hostnames common to
>> _all_ files?

>> a file example:
>> hosta
>> hostb
>> hostc
>> hostd

> Do you have duplicated hostnames in a single file?? if not, you can try
> the following awk line:

>     awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

With the same caveat of no duplicates in a single file:

        sort <all 8 files> | uniq -c | grep '^ *8'

 
 
 

grep: logical AND across multiple files?

Post by john ye » Fri, 23 Jun 2006 04:10:57




>> I have 8 files with a list of hostnames in each.

>> Is there a grep one liner that will output just the hostnames common to
>> _all_ files?

>> a file example:
>> hosta
>> hostb
>> hostc
>> hostd

> Do you have duplicated hostnames in a single file?? if not, you can try
> the following awk line:

>     awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

> Xicheng

thanks,

resolved.

 
 
 

grep: logical AND across multiple files?

Post by john ye » Fri, 23 Jun 2006 04:51:23





>>> I have 8 files with a list of hostnames in each.

>>> Is there a grep one liner that will output just the hostnames common to
>>> _all_ files?

>>> a file example:
>>> hosta
>>> hostb
>>> hostc
>>> hostd

>> Do you have duplicated hostnames in a single file?? if not, you can try
>> the following awk line:

>>     awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

> With the same caveat of no duplicates in a single file:

>     sort <all 8 files> | uniq -c | grep '^ *8'

thanks jon
 
 
 

grep: logical AND across multiple files?

Post by Sash » Sat, 24 Jun 2006 01:20:20





> >> I have 8 files with a list of hostnames in each.

> >> Is there a grep one liner that will output just the hostnames common to
> >> _all_ files?

> >> a file example:
> >> hosta
> >> hostb
> >> hostc
> >> hostd

> > Do you have duplicated hostnames in a single file?? if not, you can try
> > the following awk line:

> >     awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

> With the same caveat of no duplicates in a single file:

>    sort <all 8 files> | uniq -c | grep '^ *8'

I checked the man page for 'comm' and it seems to work for only 2
files.
Out of curiosity is there a version that will work for an arbitrary
number of files?
Regards,
Sashi
 
 
 

grep: logical AND across multiple files?

Post by John DuBo » Sun, 25 Jun 2006 05:51:55







>> >> I have 8 files with a list of hostnames in each.

>> >> Is there a grep one liner that will output just the hostnames common to
>> >> _all_ files?

>> >> a file example:
>> >> hosta
>> >> hostb
>> >> hostc
>> >> hostd

>> > Do you have duplicated hostnames in a single file?? if not, you can try
>> > the following awk line:

>> >     awk '{ k[$1]++ }END{ for(i in k){ if(k[i] == 8) print i}}' *.list

>> With the same caveat of no duplicates in a single file:

>>        sort <all 8 files> | uniq -c | grep '^ *8'

>I checked the man page for 'comm' and it seems to work for only 2
>files.
>Out of curiosity is there a version that will work for an arbitrary
>number of files?

ftp://ftp.armory.com/pub/scripts/ucomm

Description at ftp://ftp.armory.com/pub/scripts/help_pages/ucomm

The above would be:

ucomm \~ all-your-files

Works without needing to sort the input, and regardless of whether lines
occur multiple times in the files.
Requires gawk.

        John
--

 
 
 

1. Inserting text across multiple directories and files

I have a file structure thus:

/contacts/[directories a to z]/[hundreds of files
in each directory]

I need to check if the first word on the first
line of every file is 'source' and if is append a
new text line before it e.g.

existing: Line 1    source

new:      Line 1    new text
          Line 2    source

I have access to sed and awk but I am not sure how
to handle such a task across many directories.

Could anyone give me some guidance please.

Thanks in advance

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Samba and PMFirewall

3. 2.5.62-mm2 slow file system writes across multiple disks

4. Reading is much more interesting than TV (1419/1708)

5. Printing very wide files across multiple pages

6. delayed login prompt thru' telnet ??

7. Spanning file systems across multiple disks

8. Olicom RapidFire ATM PCI 155 ADAPTER 6151

9. spaning one file across multiple floppies

10. Grep/awk/sed for multiple/groups of lines in a file???

11. grep-like search with multiple file output?

12. Grep/awk/sed for multiple/groups of lines in a file???