Extract lines from file using keywords using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract lines from file using keywords using script
# 1  
Old 02-14-2013
Extract lines from file using keywords using script

Hi

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

Inputfile

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

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

Last edited by vbe; 02-14-2013 at 09:37 AM.. Reason: code tags please not HTML...
# 2  
Old 02-14-2013
Code:
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


Last edited by Yoda; 02-14-2013 at 10:50 AM..
This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-14-2013
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.

HTML Code:
/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

HTML Code:
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,  
# 4  
Old 02-14-2013
Quote:
Originally Posted by surender reddy
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.
I edited my post, try that code instead.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-15-2013
Hi bipinajith

Thanks for help.

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

HTML 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

HTML Code:
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.


HTML 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 IS/ {
        IP=$3 ;print IP;
} /context/ {
        sub(/.*]/,x);
        sub(/#.*/,x);
        print;
}' n1
output file

HTML Code:
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.
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.


---------- 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.

HTML Code:
                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   

Last edited by vbe; 02-14-2013 at 11:36 AM..
# 6  
Old 02-15-2013
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
Code:
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[i], MAX[i], NUM[i]}
        ' 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
.
.
.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

2. Shell Programming and Scripting

Display lines between keywords inclusive

Hi, I have a file that contains a lot of lines that are repetitive. I am interested in grepping the following section which is repetitive. ::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::: <Address> <Street>xxxy</Street> ... (3 Replies)
Discussion started by: jakSun8
3 Replies

3. Shell Programming and Scripting

Extract word between two KEYWORDS

Hi I want to extract all the words between two keywords HELLO & BYE. eg: Input 1_HELLO_HOW_ARE_YOU_BYE_TEST 1_HELLO_WHERE_ARE_BYE_TEST 1_HELLO_HOW_BYE_TEST Output Required: HOW_ARE_YOU WHERE_ARE HOW (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

Script needed to extract few lines from file

Hello, I need a utility script or command that will extract the following lines from a file based on a 'word' contain in a line. For example my file contains lot of lines. So if i pass 1800182 to the script/command it should return everything between 1st RequestNetRates tag before it and 1st... (4 Replies)
Discussion started by: jakSun8
4 Replies

5. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

6. UNIX for Dummies Questions & Answers

Reading specific lines from a file using index or keywords

Hello I want to read from a file which contains email addresses. The file format is like this. from@mail.com to1@mail.com to2@mail.com cc@mail.com bcc@mail.com I'll have to read from such file and assign the email addresses to respective variables. frommail =... (11 Replies)
Discussion started by: Kyaw Lwin Phyo
11 Replies

7. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

8. Shell Programming and Scripting

Sorting lines based on keywords for MySQL script

the thing which i require is very very complex.. i tried hard to find the solution but couldnt.. the thing i need to achieve is say i have a file cat delta.sql CREATE VIEW Austin Etc etc . . . CREATE VIEW Barabara AS SELECT blah blah blah FROM Austin z, Cluster s, Instance i WHERE... (4 Replies)
Discussion started by: vivek d r
4 Replies

9. Shell Programming and Scripting

Script that extract some lines from a file lookin into another

I want a ksh script that parse two files (text files, actually my original files are .xls) - input data: - one file file1 containig lines separated by spaces (or other delimiter) - 2nd one file2 contain only one numerical value in a line (for simplicity but it might of the same form as the 1st)... (7 Replies)
Discussion started by: heartwork
7 Replies

10. Shell Programming and Scripting

Capture lines based on keywords

Hello everyone, I am trying to write a script that will capture few lines from a text file based on 2 keywords in the first line and 1 keyword in the last one. It could also be based on the first line only + the folllowing 3 lines. Could some one help or give directions. Thanks. (4 Replies)
Discussion started by: nimo
4 Replies
Login or Register to Ask a Question