The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



Thread: File filter
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 09-06-2007
robotronic's Avatar
robotronic robotronic is offline
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Basically, the logic is:

Line 1) Through command line, pass to the awk script the value of the msc number to find. You can also define this variable in the body of the script if you want.

Lines 2-6) When you find a line beginning with "<", extract the msisdn number. The first split will generate the array "a", which contains two string elements: the first part is "<hgsdp:msisdn", the second part is "923228719047,loc;".
The second split takes in input the second element of the "a" array and creates a "b" array by dividing the string, using the "," delimiter. So, the first element of array "b" is the number we need.
Assuming the msisdn numbers are all 12 chars in length, we could have used a much more simpler function: substr($0, 15, 12).

Lines 7-9) When you find a line beginning with "VLR", jump to the next line. Here, in the 2nd field, we have the msc number referring to the msisdn found before.

Lines 10-12) If the msc found is equal to the msc we specified in the command line, print the msisdn number.

Line 13) The input file to feed the awk script

Code:
 1   nawk -v "in_msc=923210002002" '
 2      /^</ {
 3         split($0, a, "=");
 4         split(a[2], b, ",");
 5         msisdn=b[1];
 6      }
 7      /^VLR/ {
 8         getline;
 9         msc=$2;
10
11         if (msc == in_msc) { print(msisdn); }
12      }
13   ' input_file.txt


Well, now that I've re-read the script, it is possible to use the same logic of lines 7-9 to extract the msisdn:

Code:
nawk -v "in_msc=923210002002" '
   /^MSISDN/ {
      getline;
      msisdn=$1;
   }
   /^VLR/ {
      getline;
      msc=$2;

      if (msc == in_msc) { print(msisdn); }
   }
' input_file.txt
As usual, there will be another bunch of methods to extract the same information, maybe in a better way... The important thing is getting the result
Reply With Quote