Sponsored Content
Top Forums Shell Programming and Scripting Extract lines from file using keywords using script Post 302770060 by surender reddy on Thursday 14th of February 2013 11:24:47 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 04:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy