Extract lines from file using keywords using script

Hi

I need some lines of text from input file using keywords.

Inputfile

IP IS 10.238.52.65
[local]pun-ras-bng-mhs-01#context  bsnl.in

Card Status :  1:0,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:1, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:  4096,  

Num Circuits (L2TP):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (MIP HA):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (MIP FA):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (OTHER):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  

Num Circuits ((null)):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  


IP IS 10.238.56.225
[local]ngp-ras-bng-cto-01#context  bsnl.in


Card Status :  1:1,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1: 32768,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (L2TP):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (MIP HA):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (MIP FA):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (OTHER):
               1:     0,   2:     0,   3:     0,   4:     0,   5:     0,  
               6:     0,   7:     0,   8:     0,   9:     0,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits ((null)):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Required output will be like below

10.238.52.65

pun-ras-bng-mhs-01

Card Status :  1:0,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:1, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:  4096,  

Num Circuits (TOTAL):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  


10.238.56.225

pun-ras-bng-mhs-01

Card Status :  1:1,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1: 32768,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  



Num Circuits (TOTAL):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

keywords are IP IS,cont , Card Status (two lines with this key word).
Max Circuits(two lines with this key word).
TOTAL(two lines with this key word).

can anybody help to get output

awk '/^[a-zA-Z]/ {
        if(/^Card Status/||/^Max Circuits/||/^Num Circuits \(TOTAL\)/)
                f=1;
        else
                f=0;
} f==1 {
        print;
} /^IP/ {
        print $NF;
} /#/ {
        sub(/.*]/,x);
        sub(/#.*/,x);
        print;
}' input_file
1 Like

Hi ,
code is working fine but i need ip or host name in output file .now iam not able to difffernaite which output is belongs to which router.

/usr/xpg4/bin/awk '/^[a-zA-Z]/{if(/^Card Status/||/^Max Circuits/||/^Num Circuits \(TOTAL\)/) f=1; else f=0;}f==1{print}' n1 

output

Card Status :  1:0,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:1, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:  4096,  

Num Circuits (TOTAL):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  

Card Status :  1:1,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1: 32768,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Card Status :  1:0,  2:1,  3:1,  4:0,  5:1,  6:1,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4:     0,   5: 32768,  
               6: 32768,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:     0,   2: 18651,   3:  6325,   4:     0,   5:     4,  
               6:     1,   7:     1,   8:     0,   9:    13,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

I edited my post, try that code instead.

1 Like

Hi bipinajith

Thanks for help.

output is coming but with some more extra lines after executing your code

/usr/xpg4/bin/awk '/^[a-zA-Z]/ {
        if(/^Card Status/||/^Max Circuits/||/^Num Circuits \(TOTAL\)/)
                f=1;
        else
                f=0;
} f==1 {
        print;
} /^IP/ {
        print $NF;
} /#/ {
        sub(/.*]/,x);
        sub(/#.*/,x);
        print;
}' n1

Output

10.238.52.65
pun-ras-bng-mhs-01
pun-ras-bng-mhs-01
pun-ras-bng-mhs-01
Card Status :  1:0,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:1, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:  4096,  

Num Circuits (TOTAL):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  

0
pun-ras-bng-mhs-01
10.238.54.1
enk-ras-bng-cse-01
enk-ras-bng-cse-01
enk-ras-bng-cse-01
Card Status :  1:1,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1: 32768,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

0
enk-ras-bng-cse-01
10.238.56.225
ngp-ras-bng-cto-01
ngp-ras-bng-cto-01
ngp-ras-bng-cto-01
Card Status :  1:0,  2:1,  3:1,  4:0,  5:1,  6:1,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4:     0,   5: 32768,  
               6: 32768,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:     0,   2: 18651,   3:  6325,   4:     0,   5:     4,  
               6:     1,   7:     1,   8:     0,   9:    13,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

0
ngp-ras-bng-cto-01

I made small change in code ,used different key word ,now output is coming as desired.

/usr/xpg4/bin/awk '/^[a-zA-Z]/ {
        if(/^Card Status/||/^Max Circuits/||/^Num Circuits \(TOTAL\)/)
                f=1;
        else
                f=0;
} f==1 {
        print;
} /IP IS/ {
        IP=$3 ;print IP;
} /context/ {
        sub(/.*]/,x);
        sub(/#.*/,x);
        print;
}' n1

output file

10.238.52.65
pun-ras-bng-mhs-01
Card Status :  1:0,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:1, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:  4096,  

Num Circuits (TOTAL):
               1:     4,   2:  8959,   3: 10591,   4:     4,   5:     4,  
               6:     0,   7:     1,   8:     0,   9:    12,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     1,  

10.238.54.1
enk-ras-bng-cse-01
Card Status :  1:1,  2:1,  3:1,  4:1,  5:0,  6:0,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1: 32768,   2: 32768,   3: 32768,   4: 32768,   5:     0,  
               6:     0,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:  4916,   2: 10976,   3:  9385,   4:     7,   5:     0,  
               6:     0,   7:     1,   8:     0,   9:    14,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

10.238.56.225
ngp-ras-bng-cto-01
Card Status :  1:0,  2:1,  3:1,  4:0,  5:1,  6:1,  7:0, 
               8:0,  9:1, 10:0, 11:0, 12:0, 13:0, 14:0, 

Max Circuits:  1:     0,   2: 32768,   3: 32768,   4:     0,   5: 32768,  
               6: 32768,   7:     0,   8:     0,   9: 16384,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

Num Circuits (TOTAL):
               1:     0,   2: 18651,   3:  6325,   4:     0,   5:     4,  
               6:     1,   7:     1,   8:     0,   9:    13,  10:     0,  
              11:     0,  12:     0,  13:     0,  14:     0,  

thanks again ,you made my work easier.

---------- Post updated at 11:24 PM ---------- Previous update was at 10:27 AM ----------

Hi

any body can help to get the same output like in below format from input file in previous post.

                IP   HOST NAME           Card No       card status   Maximum circuits   Total circuits  
                        10.238.48.1   bgl-ras-bng-bge-01   1   1   32768   6628   
                         10.238.48.1   bgl-ras-bng-bge-01   2   1   32768   8186     
                          0.238.48.1   bgl-ras-bng-bge-01   3   1   32768   4    
                        10.238.48.1   bgl-ras-bng-bge-01   4   1   32768   5247     
                        10.238.48.1   bgl-ras-bng-bge-01   5   0   0   0     
                        10.238.48.1   bgl-ras-bng-bge-01   6   0   0   0      
                         10.238.48.1   bgl-ras-bng-bge-01   7   0   0   0      
                         10.238.48.1   bgl-ras-bng-bge-01   8   0   0   0      
                         10.238.48.1   bgl-ras-bng-bge-01   9   1   16384   124 
                         10.238.48.1   bgl-ras-bng-bge-01   10   0   0   0    
                         10.238.48.1   bgl-ras-bng-bge-01   11   0   0   0     
                         10.238.48.1   bgl-ras-bng-bge-01   12   0   0   0      
                         10.238.48.1   bgl-ras-bng-bge-01   13   0   0   0    
                         10.238.48.1   bgl-ras-bng-bge-01   14   1   4096   1   

What about opening a new thread, telling us what to do instead of leaving us guessing what goes where based on which criterion?

---------- Post updated at 10:27 ---------- Previous update was at 09:29 ----------

Well, trying and interpreting and guessing, I've come up with

awk     'BEGIN {print "IP\tHOST\tCARD\tSTATUS\tMAX\tTOT"}
         function prep (Ar)     {tmp=$0
                                 while ($0 != "") {getline; tmp=tmp $0}
                                 gsub (/[A-Za-z ()0-9]*: *|, *$/,"",tmp)
                                 return split (tmp, Ar, ",")}
         /^IP/ {IP = $NF}
         /^.local/ {gsub(/.local.|#context/, "", $1); HOST=$1}
         /^Card Status/ {n = prep (STAT) }
         /^Max Circuit/ {n = prep (MAX)  }
         /^Num.*TOTAL/  {n = prep (NUM);
                         for (i=1; i<=n; i++) print IP, HOST, i, STAT, MAX, NUM}
        ' OFS="\t" file
IP    HOST    CARD    STATUS    MAX    TOT
10.238.52.65    pun-ras-bng-mhs-01    1    0    0    4
10.238.52.65    pun-ras-bng-mhs-01    2    1    32768    8959
10.238.52.65    pun-ras-bng-mhs-01    3    1    32768    10591
.
.
.
10.238.52.65    pun-ras-bng-mhs-01    14   1    4096    1
10.238.56.225   ngp-ras-bng-cto-01    1    1    32768    4916
10.238.56.225   ngp-ras-bng-cto-01    2    1    32768    10976
.
.
.