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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to search string and number in one file and check in the other file
# 8  
Old 08-23-2007
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
# 9  
Old 08-23-2007
Code:
$ 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.
# 10  
Old 08-24-2007
Quote:
Originally Posted by summer_cherry (private message)
Hi,
For this code, i think it is really smart one. But there are some confused point for me. Would you pls explain it in detail for me. Thanks advanced.

1> if you send both file1 and file2 to awk, what will be the execute logic?
2> it seems
part [NR==FNR && /Not exist/ ] is for file1
part [/:$/ ] is for file2
part[NF==10 ]is for file1
is it true?

Actually, my concern is the execute logic for awk that take two files as input.

Thank you very much.
Code:
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

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 :
Code:
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)
}

Code:
$ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Search a string in a file which is also present in another file in UNIX

Hi there, I am new to Unix and had below requirement to finish my task. I have file1.dat which has data as shown below. case1.txt case2.txt case3.txt case4.txt file1.dat has only file names I have folder which has above files mentioned in file1.dat ./all_files case1.txt... (6 Replies)
Discussion started by: raj028
6 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

6. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

7. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

8. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

10. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies
Login or Register to Ask a Question