Select lines from a file based on a criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select lines from a file based on a criteria
# 1  
Old 02-02-2013
Select lines from a file based on a criteria

Hi

I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate file.

thanks in advance

my file looks like this

Code:
HOME SUBSCRIBER IDENTIFICATION HANDLING COMMAND <MI_>
<  ZMIO:MSISDN=12345678:;

LOADING PROGRAM VERSION 13.18-0

HLRi      DM1HLR01                  2013-01-31  18:27:51

                SUBSCRIBER INFORMATION:

        INTERNATIONAL MOBILE SUBSCRIBER IDENTITY ... 4324567891234 
        MOBILE STATION ISDN NUMBER ................. 12345678    
        ATTACHED IMSI ..............................                 
        MOBILE STATION CATEGORY .................... OR  
        ROUTING CATEGORY ........................... 22 
        ADDITIONAL ROUTING CATEGORY ................ N    
        SERVICE AREA OF MSISDN ..................... ALL
        ACTIVATION STATUS .......................... A    
        VLR-ADDRESS ................................                 
        SIGNALLING POINT CODE ...................... 00000000
        MSC-ADDRESS ................................                 
        PRIMARY BASIC SERVICE CODE ................. T11
        PRIMARY BASIC SERVICE CODE INDEX ........... 000
        ROAMING PROFILE INDEX ...................... 26
        FRAUD PROFILE .............................. N  
        CALLING LINE IDENTIFICATION ENHANCEMENT .... N
        COMMON MSISDN NUMBER ....................... N               
        OVERRIDE COMMON CLI PARAMETERS ............. N
        CMSISDN IS HUNTING GROUP NUMBER ............ N
        DENY DIRECT CALLS .......................... N
        DENY USSD WITH MEMBER NUMBER ............... N
        DENY DIRECT SMS ............................ N

        ZONE CODES:
                 

        MSC AREA RESTRICTED ........................ N

        HLRU IDENTITY .............................. 6

        HOME COUNTRY CODE .......................... N  
        NETWORK DESTINATION CODE ................... N    


COMMAND EXECUTED


<  ZMIO:MSISDN=98765432134:;

LOADING PROGRAM VERSION 13.18-0

HLRi      DM1HLR01                  2013-01-31  18:27:54

                SUBSCRIBER INFORMATION:

        INTERNATIONAL MOBILE SUBSCRIBER IDENTITY ... 32134567895467 
        MOBILE STATION ISDN NUMBER ................. 98765432134 
        ATTACHED IMSI ..............................                 
        MOBILE STATION CATEGORY .................... OR  
        ROUTING CATEGORY ........................... 52 
        ADDITIONAL ROUTING CATEGORY ................ N    
        SERVICE AREA OF MSISDN ..................... ALL
        ACTIVATION STATUS .......................... A    
        VLR-ADDRESS ................................ 96431324567 
        SIGNALLING POINT CODE ...................... 00000000
        MSC-ADDRESS ................................ 543213456789    
        PRIMARY BASIC SERVICE CODE ................. T22
        PRIMARY BASIC SERVICE CODE INDEX ........... 000
        ROAMING PROFILE INDEX ...................... 6 
        FRAUD PROFILE .............................. N  
        CALLING LINE IDENTIFICATION ENHANCEMENT .... N
        COMMON MSISDN NUMBER ....................... N               
        OVERRIDE COMMON CLI PARAMETERS ............. N
        CMSISDN IS HUNTING GROUP NUMBER ............ N
        DENY DIRECT CALLS .......................... N
        DENY USSD WITH MEMBER NUMBER ............... N
        DENY DIRECT SMS ............................ N

        ZONE CODES:
                 

        MSC AREA RESTRICTED ........................ N

        HLRU IDENTITY .............................. 3

        HOME COUNTRY CODE .......................... N  
        NETWORK DESTINATION CODE ................... N    


COMMAND EXECUTED


Last edited by Scott; 02-02-2013 at 09:03 AM.. Reason: Please use code tags
# 2  
Old 02-02-2013
PLEASE use code tags as demanded in forum rules!
If the order of the keys is always fixed and as you stat above, try
Code:
awk '/ZMIO:MSISDN|MOBILE STATION ISDN NUMBER/ {printf "%s%s", $0, OFS}    # either pattern matched, print to same line
     /VLR-ADDRESS/                                                        # pattern matched - take default action (print $0)
    ' file
< ZMIO:MSISDN=12345678:; MOBILE STATION ISDN NUMBER .... 12345678 VLR-ADDRESS .....
< ZMIO:MSISDN=98765432134:; MOBILE STATION ISDN NUMBER .... 98765432134 VLR-ADDRESS .... 96431324567

# 3  
Old 02-02-2013
Hi

thanks for the reply,

Is there any way that I can insert a critieria that if the numerical in the line

Code:
ZMIO:MSISDN=12345678

matches

the numerical in this line

Code:
MOBILE STATION ISDN NUMBER ................. 12345678

only then the files will be selected otherwise no

Last edited by Scott; 02-02-2013 at 09:04 AM.. Reason: Code tags
# 4  
Old 02-02-2013
PLEASE use code tags as demanded in forum rules!

Code:
$ awk  '/ZMIO:MSISDN/                  {tmp1 = $0; gsub(/[^0-9]/,"", $NF); key1 = $NF}
        /MOBILE STATION ISDN NUMBER/   {tmp2 = $0; gsub(/[^0-9]/,"", $NF); key2 = $NF}
        /VLR-ADDRESS/ && (key1==key2)  {printf "%s%s%s%s%s\n", tmp1, OFS, tmp2, OFS, $0}
       ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove lines from File.A based on criteria in File.B

Hello, I have two files of the following form. I would like to remove from File.A where the first three colum matches values in File.B to give the output in File.C File.A 121 54321 PQR CAT 122 765431 ABC DOG 124 98765 ZXY TIGER 125 86432 GEF LION File.B 122 765431 ABC 125 86432 GEF... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

2. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies

3. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

4. UNIX for Dummies Questions & Answers

How to select files based on a criteria?

I have a file..... xxx 2345 455 abc 345 555 cdf 456 777 fff 555 888 Now my requirement is, Say if, i want to select only those records prior to the record fff 555 888... how do i go about doing this in unix.... The fff would be hardcoded as it wud be fixed and everytime when i... (7 Replies)
Discussion started by: saggiboy10
7 Replies

5. UNIX for Dummies Questions & Answers

how to select lines from one file based on another file

Hi, I would like to know how can I select lines of one file based on a common ID column from another file (keeping the order of the second file). Example of file1: ID A B C D 1-30 1 2 3 5-60 4 5 6 1-20 7 8 9 Example of file2: ID chr pos 1-20 1 20 1-30 1 30 5-60 5 60 Desired... (2 Replies)
Discussion started by: fadista
2 Replies

6. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

7. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

8. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

9. Shell Programming and Scripting

remove lines based on score criteria

Hi guys, Please guide for Solution. PART-I INPUT FILE (has 2 columns ID and score) TC5584_1 93.9 DV161411_2 79.5 BP132435_5 46.8 EB682112_1 34.7 BP132435_4 29.5 TC13860_2 10.1 OUTPUT FILE (It shudn't contain the line ' BP132435_4 29.5 ' as BP132435 is repeated... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question