The UNIX and Linux Forums  

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


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to filter out some paragraphs in a file cnlhap Shell Programming and Scripting 7 08-19-2008 12:03 PM
filter the string from a file ?? varungupta Shell Programming and Scripting 11 09-17-2007 07:11 PM
how do I filter double lines from a txt file I-1 Shell Programming and Scripting 10 02-20-2007 09:21 AM
filter parts of a big file using awk or sed script apalex Shell Programming and Scripting 1 07-25-2005 01:45 PM
filter out certain column from a file CamTu Shell Programming and Scripting 4 04-04-2005 03:24 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-05-2007
Registered User
 

Join Date: Jul 2006
Posts: 47
Arrow File filter

Hi Everyone , have a nice
i would need a little help on this
i have file which contains blocks such as given below

<hgsdp:msisdn=923228719047,loc;
HLR SUBSCRIBER DATA

SUBSCRIBER IDENTITY
MSISDN IMSI STATE AUTHD
923228719047 410072110070614 CONNECTED AVAILABLE

NAM
1

LOCATION DATA
VLR ADDRESS MSRN MSC NUMBER LMSID
4-923210002011 923210002011
MS PURGED IN VLR

END
<hgsdp:msisdn=923228276174,loc;
HLR SUBSCRIBER DATA

SUBSCRIBER IDENTITY
MSISDN IMSI STATE AUTHD
923228276174 410072520066962 CONNECTED AVAILABLE

NAM
1

LOCATION DATA
VLR ADDRESS MSRN MSC NUMBER LMSID
4-923210002002 923210002002
MS PURGED IN VLR

END

now i want to filter out MSISDN based on MSC NUMBER ( bold above )
like i only want those MSISDNs in my output file which has MSC NUMBER = 923210002022

Thanks in Anticipation
Regards
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-06-2007
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Code:
nawk -v "in_msc=923210002002" '
   /^</ {
      split($0, a, "=");
      split(a[2], b, ",");
      msisdn=b[1];
   }
   /^VLR/ {
      getline;
      msc=$2;

      if (msc == in_msc) { print(msisdn); }
   }
' input_file.txt
Reply With Quote
  #3 (permalink)  
Old 09-06-2007
Registered User
 

Join Date: Jul 2006
Posts: 47
Thanks its working like charm

but would just describe this code line by line , so that i don need to tele the whole output , whould just analyze code and this would be enuff to make sure that output is correct

Regards and Thanks
Reply With Quote
  #4 (permalink)  
Old 09-06-2007
robotronic's Avatar
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
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0