how to search string and number in one file and check in the other file

Hi,
Can anyone help in the below problem.
file1 has the below contents

fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist
fileset 999 primary-ilist inode 115016 dup block -> /export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgnome-l10nmessages-ptBR/archive
fileset 999 primary-ilist inode 116366 dup block -> /export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgscr/archive
fileset 999 primary-ilist inode 115016 dup block -> /export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgnome-libs/reloc/lib/libglade
fileset 999 primary-ilist inode 116366 dup block ->
fileset 999 primary-ilist inode 116387 dup block
fileset 999 primary-ilist inode 116400 dup block
fileset 999 primary-ilist inode 116435 dup block
fileset 999 primary-ilist inode 116441 dup block
fileset 999 primary-ilist inode 116510 dup block
fileset 999 primary-ilist inode 116521 dup block
fileset 999 primary-ilist inode 116570 dup block
fileset 999 primary-ilist inode 116614 dup block
fileset 999 primary-ilist inode 116616 dup block
fileset 999 primary-ilist inode 29275 dup block
fileset 999 primary-ilist inode 30193 dup block
fileset 999 primary-ilist inode 30318 dup block
fileset 999 primary-ilist inode 30321 dup block
fileset 999 primary-ilist inode 30334 dup block
fileset 999 primary-ilist inode 30336 dup block
fileset 999 primary-ilist inode 30340 dup block
fileset 999 primary-ilist inode 30343 dup block
fileset 999 primary-ilist inode 30344 dup block
fileset 999 primary-ilist inode 30346 dup block
fileset 999 primary-ilist inode 30907 dup block
fileset 999 primary-ilist inode 31026 dup block
fileset 999 primary-ilist inode 31028 dup block
fileset 999 primary-ilist inode 31032 dup block
fileset 999 primary-ilist inode 31035 dup block
fileset 999 primary-ilist inode 31037 dup block
fileset 999 primary-ilist inode 31040 dup block
fileset 999 primary-ilist inode 31041 dup block
fileset 999 primary-ilist inode 31042 dup block
fileset 999 primary-ilist inode 31044 dup block
fileset 999 primary-ilist inode 31045 dup block
fileset 999 primary-ilist inode 31050 dup block
fileset 999 primary-ilist inode 31068 dup block
fileset 999 primary-ilist inode 31180 dup block
fileset 999 primary-ilist inode 31479 dup block
fileset 999 primary-ilist inode 31538 dup block
fileset 999 primary-ilist inode 31558 dup block
fileset 999 primary-ilist inode 31622 dup block

file2 has the below contents
/export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgpch:
total 70
81524 drwxrwxr-x 5 root staff 1024 Jun 29 2004 .
79918 drwxr-xr-x 1098 root bin 32768 Dec 6 2005 ..
81525 drwxr-xr-x 2 root staff 96 Jun 29 2004 archive
116357 drwxr-xr-x 2 root staff 96 Jun 29 2004 install
116361 -rw-r--r-- 1 root staff 460 Jun 29 2004 pkginfo
116362 -rw-r--r-- 1 root staff 225 Jun 29 2004 pkgmap
116363 drwxr-xr-x 3 root staff 96 Jun 29 2004 reloc

/export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgpch/archive:
total 88
81525 drwxr-xr-x 2 root staff 96 Jun 29 2004 .
81524 drwxrwxr-x 5 root staff 1024 Jun 29 2004 ..
116356 -rw-r--r-- 1 root staff 43670 Mar 2 2002 none.bz2

/export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgpch/install:
total 12
116357 drwxr-xr-x 2 root staff 96 Jun 29 2004 .
81524 drwxrwxr-x 5 root staff 1024 Jun 29 2004 ..
116358 -rw-r--r-- 1 root staff 798 Jan 10 2002 copyright
116359 -rw-r--r-- 1 root staff 875 Mar 2 2002 depend
116360 -rw-r--r-- 1 root staff 2245 Mar 2 2002 i.none
/export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgsdhx/archive:
total 50
86697 drwxr-xr-x 2 root staff 96 Jun 29 2004 .
86696 drwxrwxr-x 5 root staff 1024 Jun 29 2004 ..
86698 -rw-r--r-- 1 root staff 24419 Apr 18 2002 none.bz2

/export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgsdhx/install:
total 12
86699 drwxr-xr-x 2 root staff 96 Jun 29 2004 .
86696 drwxrwxr-x 5 root staff 1024 Jun 29 2004 ..
86700 -rw-r--r-- 1 root staff 93 Apr 7 2002 copyright
86701 -rw-r--r-- 1 root staff 724 Apr 6 2002 depend
86702 -rw-r--r-- 1 root staff 2245 Apr 18 2002 i.none

I have to check '37020' from file1 and check exist in file2, if it exists print the file path
how to do this????????

The following script searches in file2 the inode �Not exist� of file1 :

awk '
NR==FNR && /Not exist/ {
   Files[$5] = $0;
   next;
}
/:$/ {
   Path = substr($0, 1, length-1);
   next;
}
NF==10 {
   if ($1 in Files) {
      sub(/->.*/, "", Files[$1])
      print Files[$1] "-> " Path "/" $10;
      delete Files[$1];
   }
}
END {
   for (inode in Files) {
      print Files[inode];
   }
} ' file1 file2

I had the following record to file1:

fileset 999 primary-ilist inode 116358 has invalid dotdot (426094) -> Not exist

And the result is:

fileset 999 primary-ilist inode 116358 has invalid dotdot (426094) -> /export/jumpstart/solaris9_image/Solaris_9/Product/SUNWgpch/in
stall/copyright
fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist

Jean-Pierre.

Try and adapt the following script :

awk '

# First file
NR==FNR {
   if (/:$/)
      Paths[++PathsCount] = substr($0, 1, length-1);
   else if (NF==10)
      Inodes[$1] = PathsCount;
   next;
}

# Other files

NF>1 {
   if ($5 in Inodes)
      path = Paths[Inodes[$5]]
   else
      path = "Not exist";
   print $0,"->",path;
}

' file2.log file1.log

With your sample datas posted, the result is :

fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist
fileset 999 primary-ilist inode 115016 dup block -> Not exist
fileset 999 primary-ilist inode 116614 dup block -> /export/jumpstart/solaris9_image/Solaris_9/Product/SUNWimagick/install
fileset 999 primary-ilist inode 116616 dup block -> Not exist

#!/bin/sh/

awk '

# First file
NR==FNR {
if (/:$/)
Paths[++PathsCount] = substr($0, 1, length-1);
else if (NF==10)
Inodes[$1] = PathsCount;
next;
}

# Other files

NF>1 {
if ($5 in Inodes)
path = Paths[Inodes[$5]]
else
path = "Not exist";
print $0,"->",path;
}

' file2.log file1.log

when i execute this i am getting the below error: can you pl. help me in this

qpadmka >./test3.sh
awk: syntax error near line 5
awk: illegal statement near line 5
awk: syntax error near line 7
awk: illegal statement near line 7
awk: syntax error near line 15
awk: illegal statement near line 15
awk: syntax error near line 17
awk: illegal statement near line 17

Try nawk instead of awk.

Jean-Pierre.

Thanks a lot it worked...........but i have one more problem came across

suppose in file1.log i have the below content:

fileset 999 primary-ilist inode 31538 dup block
fileset 999 primary-ilist inode 31558 dup block
fileset 999 primary-ilist inode 31622 dup block
fileset 999 primary-ilist inode 32785 dup block
fileset 999 primary-ilist inode 36992 dup block
fileset 999 primary-ilist inode 36994 dup block

in file2.log has the below contents:
/export/var/opt/novell/edirectory:
total 768860
33365 drwxr-xr-x 6 root root 1024 Nov 17 2006 .
65 drwxr-xr-x 3 root other 96 Sep 1 2006 ..
32793 -rw------- 1 root root 194379776 Sep 1 2006 NDO.01
32792 -rw------- 1 root root 262144 Sep 1 2006 NDO.db
32782 -rw------- 1 root root 0 Sep 1 2006 NDO.lck
32794 drwx------ 2 root root 96 Sep 1 2006 NDO.rfl
37480 drwxr-xr-x 2 root root 96 Nov 24 2005 StatLog
32791 -rw-r--r-- 1 root root 132 Nov 17 2006 _ndsdb.ini
32781 -rw-r--r-- 1 root root 403 Nov 24 2005 attributeIndex.ldif
32776 -rw-r--r-- 1 root root 13218 Nov 24 2005 attributeSchema.ldif
32779 drw------- 3 root root 96 Nov 24 2005 certserv
32785 -rw------- 1 root root 198705152 Aug 22 2007 nds.01

when a execute the script i get the output as 'not exist'

fileset 999 primary-ilist inode 32785 dup block -> Not exist

please helpme in this

The script works fine for me :

$ cat file1.log
fileset 999 primary-ilist inode 31538 dup block
fileset 999 primary-ilist inode 31558 dup block
fileset 999 primary-ilist inode 31622 dup block
fileset 999 primary-ilist inode 32785 dup block
fileset 999 primary-ilist inode 36992 dup block
fileset 999 primary-ilist inode 36994 dup block
$ cat file2.log
/export/var/opt/novell/edirectory:
total 768860
33365 drwxr-xr-x 6 root root 1024 Nov 17 2006 .
65 drwxr-xr-x 3 root other 96 Sep 1 2006 ..
32793 -rw------- 1 root root 194379776 Sep 1 2006 NDO.01
32792 -rw------- 1 root root 262144 Sep 1 2006 NDO.db
32782 -rw------- 1 root root 0 Sep 1 2006 NDO.lck
32794 drwx------ 2 root root 96 Sep 1 2006 NDO.rfl
37480 drwxr-xr-x 2 root root 96 Nov 24 2005 StatLog
32791 -rw-r--r-- 1 root root 132 Nov 17 2006 _ndsdb.ini
32781 -rw-r--r-- 1 root root 403 Nov 24 2005 attributeIndex.ldif
32776 -rw-r--r-- 1 root root 13218 Nov 24 2005 attributeSchema.ldif
32779 drw------- 3 root root 96 Nov 24 2005 certserv
32785 -rw------- 1 root root 198705152 Aug 22 2007 nds.01

$ cat knshree2.sh
awk '

# file2

NR==FNR {
   if (/:$/)
      Paths[++PathsCount] = substr($0, 1, length-1);
   else if (NF==10)
      Inodes[$1] = PathsCount;
   next;
}

# file1

NF>1 {
   if ($5 in Inodes)
      path = Paths[Inodes[$5]]
   else
      path = "Not exist";
   print $0,"->",path;
}

' file2.log file1.log
$ knshree2.sh
fileset 999 primary-ilist inode 31538 dup block -> Not exist
fileset 999 primary-ilist inode 31558 dup block -> Not exist
fileset 999 primary-ilist inode 31622 dup block -> Not exist
fileset 999 primary-ilist inode 32785 dup block -> /export/var/opt/novell/edirectory
fileset 999 primary-ilist inode 36992 dup block -> Not exist
fileset 999 primary-ilist inode 36994 dup block -> Not exist
$

Jean-Pierre.

hi Jean,

sorry to trouble you

knshree2.sh should print as below(path followed by listing all the files):

/export/var/opt/novell/edirectory:
total 768860
33365 drwxr-xr-x 6 root root 1024 Nov 17 2006 .
65 drwxr-xr-x 3 root other 96 Sep 1 2006 ..
32793 -rw------- 1 root root 194379776 Sep 1 2006 NDO.01
32792 -rw------- 1 root root 262144 Sep 1 2006 NDO.db
32782 -rw------- 1 root root 0 Sep 1 2006 NDO.lck
32794 drwx------ 2 root root 96 Sep 1 2006 NDO.rfl
37480 drwxr-xr-x 2 root root 96 Nov 24 2005 StatLog
32791 -rw-r--r-- 1 root root 132 Nov 17 2006 _ndsdb.ini
32781 -rw-r--r-- 1 root root 403 Nov 24 2005 attributeIndex.ldif
32776 -rw-r--r-- 1 root root 13218 Nov 24 2005 attributeSchema.ldif
32779 drw------- 3 root root 96 Nov 24 2005 certserv
32785 -rw------- 1 root root 198705152 Aug 22 2007 nds.01

$ cat file1.log
fileset 999 primary-ilist inode 31538 dup block
fileset 999 primary-ilist inode 31558 dup block
fileset 999 primary-ilist inode 31622 dup block
fileset 999 primary-ilist inode 32785 dup block
fileset 999 primary-ilist inode 36992 dup block
fileset 999 primary-ilist inode 36994 dup block
$ cat file2.log
/export/var/opt/novell/edirectory:
total 768860
33365 drwxr-xr-x 6 root root 1024 Nov 17 2006 .
65 drwxr-xr-x 3 root other 96 Sep 1 2006 ..
32793 -rw------- 1 root root 194379776 Sep 1 2006 NDO.01
32792 -rw------- 1 root root 262144 Sep 1 2006 NDO.db
32782 -rw------- 1 root root 0 Sep 1 2006 NDO.lck
32794 drwx------ 2 root root 96 Sep 1 2006 NDO.rfl
37480 drwxr-xr-x 2 root root 96 Nov 24 2005 StatLog
32791 -rw-r--r-- 1 root root 132 Nov 17 2006 _ndsdb.ini
32781 -rw-r--r-- 1 root root 403 Nov 24 2005 attributeIndex.ldif
32776 -rw-r--r-- 1 root root 13218 Nov 24 2005 attributeSchema.ldif
32779 drw------- 3 root root 96 Nov 24 2005 certserv
32785 -rw------- 1 root root 198705152 Aug 22 2007 nds.01

$ cat knshree3.sh
awk '

# file2

NR==FNR {
   if (/:$/)
      Paths[++PathsCount] = substr($0, 1, length-1);
   else {
      if (NF==10)
         Inodes[$1] = PathsCount;
      if (NF>0)
         Paths[PathsCount] = Paths[PathsCount] "\n" $0;
   }
   next;
}

# file1

NF>1 {
   if ($5 in Inodes) {
      print $0 "->";
      print Paths[Inodes[$5]];
   }
}

' file2.log file1.log

$ knshree3.sh
fileset 999 primary-ilist inode 32785 dup block->
/export/var/opt/novell/edirectory
total 768860
33365 drwxr-xr-x 6 root root 1024 Nov 17 2006 .
65 drwxr-xr-x 3 root other 96 Sep 1 2006 ..
32793 -rw------- 1 root root 194379776 Sep 1 2006 NDO.01
32792 -rw------- 1 root root 262144 Sep 1 2006 NDO.db
32782 -rw------- 1 root root 0 Sep 1 2006 NDO.lck
32794 drwx------ 2 root root 96 Sep 1 2006 NDO.rfl
37480 drwxr-xr-x 2 root root 96 Nov 24 2005 StatLog
32791 -rw-r--r-- 1 root root 132 Nov 17 2006 _ndsdb.ini
32781 -rw-r--r-- 1 root root 403 Nov 24 2005 attributeIndex.ldif
32776 -rw-r--r-- 1 root root 13218 Nov 24 2005 attributeSchema.ldif
32779 drw------- 3 root root 96 Nov 24 2005 certserv
32785 -rw------- 1 root root 198705152 Aug 22 2007 nds.01
$

Jean-Pierre.

When there are more than one file as input to awk, the files are read and proceed in sequence.

The variable FILENAME contains the name of the current input file.
The two variables FNR and NR are related to the record number :
FNR = The input record number in the current input file.
NR = The total number of input records seen so far.

You can distinguish the first input file from other by comparing FNR and NR. The two variables are equals only in that case.

The following awk program use multiple input files :

FILENAME != prv_FILENAME {
   prv_FILENAME = FILENAME;
   cnt_FILENAME++;
   printf("=== New input file #%d ===\n", cnt_FILENAME)
}

NR == 1 {
   print "  = First input file ===";
}

FNR == NR {
   printf("    Filename=%s, Record#=%d (from first input file)\n", FILENAME, FNR)
   next;   # Stop  processing  the current input record.
}

{
  printf("    Filename=%s, Record#=%d, Total record#=%d\n", FILENAME, FNR, NR)
}
$ date | gawk -f files.awk file1.log file2.log -
=== New input file #1 ===
  = First input file ===
    Filename=file1.log, Record#=1 (from first input file)
    Filename=file1.log, Record#=2 (from first input file)
    Filename=file1.log, Record#=3 (from first input file)
    Filename=file1.log, Record#=4 (from first input file)
    Filename=file1.log, Record#=5 (from first input file)
    Filename=file1.log, Record#=6 (from first input file)
=== New input file #2 ===
    Filename=file2.log, Record#=1, Total record#=7
    Filename=file2.log, Record#=2, Total record#=8
    Filename=file2.log, Record#=3, Total record#=9
    Filename=file2.log, Record#=4, Total record#=10
    Filename=file2.log, Record#=5, Total record#=11
    Filename=file2.log, Record#=6, Total record#=12
    Filename=file2.log, Record#=7, Total record#=13
    Filename=file2.log, Record#=8, Total record#=14
    Filename=file2.log, Record#=9, Total record#=15
    Filename=file2.log, Record#=10, Total record#=16
    Filename=file2.log, Record#=11, Total record#=17
    Filename=file2.log, Record#=12, Total record#=18
    Filename=file2.log, Record#=13, Total record#=19
    Filename=file2.log, Record#=14, Total record#=20
    Filename=file2.log, Record#=15, Total record#=21
=== New input file #3 ===
    Filename=-, Record#=1, Total record#=22
$

Jean-Pierre.