Count lines in section


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count lines in section
# 1  
Old 10-28-2015
Count lines in section

I am tiring to cont numbers of line between the "!" in CISCO routers
I have no problem to extract the input and change the empty line with !

Code:
!
5                Cable5/0/1 U0    4            
5                Cable5/0/1 U1    4            
!
5                Cable5/0/1 U2    4            
5                Cable5/0/1 U7    4            
9                Cable5/0/2 U0    8            
!
9                Cable5/0/2 U1    8            
!
9                Cable5/0/2 U4    8            
9                Cable5/0/2 U5    8            
!

The output will be
Code:
2
3
1
2

And another question
I need to get the content of the line for example
I need to get al the section wit 2 lines

The output will be

Code:
5                Cable5/0/1 U0    4            
5                Cable5/0/1 U1    4  

9                Cable5/0/2 U4    8            
9                Cable5/0/2 U5    8


Thanks
Sharon

Last edited by Don Cragun; 10-28-2015 at 06:23 AM.. Reason: Add CODE tags.
# 2  
Old 10-28-2015
Please let us know what you've tried. It would be easier for forum members to help you a bit rather than work on a solution to your question from scratch :-)
Also do post details such as which OS, shell, preferred tool (bash/awk/sed/perl), etc...
# 3  
Old 10-28-2015
Append an exclamation sign at EOF. and try this :

Code:
 awk 'BEGIN{a=0}{if($0 == "!"){print a;a=0} else{a+=1; }}' abc.txt

Here abc.txt is the input file.
# 4  
Old 10-28-2015
Quote:
Originally Posted by sharong
I am tiring to cont numbers of line between the "!" in CISCO routers
I have no problem to extract the input and change the empty line with !

!
5 Cable5/0/1 U0 4
5 Cable5/0/1 U1 4
!
5 Cable5/0/1 U2 4
5 Cable5/0/1 U7 4
9 Cable5/0/2 U0 8
!
9 Cable5/0/2 U1 8
!
9 Cable5/0/2 U4 8
9 Cable5/0/2 U5 8
!

The output will be
2
3
1
2

And another question
I need to get the content of the line for example
I need to get al the section wit 2 lines

The output will be

5 Cable5/0/1 U0 4
5 Cable5/0/1 U1 4

9 Cable5/0/2 U4 8
9 Cable5/0/2 U5 8


Thanks
Sharon
Hello sharong,

Following may help you in same.
1st requirement:
Code:
awk '($0 ~ /^\!$/){if(A){print A;A=""};next} {;A++}' Input_file

Output will be as follows.
Code:
2
3
1
2

2nd requirement:
Code:
awk '($0 ~ /^\!$/){if(A==2){print O};A="";O="";next} {O=O?O ORS $0:$0;A++}'  Input_file

Output will be as follows.
Code:
5 Cable5/0/1 U0 4
5 Cable5/0/1 U1 4
9 Cable5/0/2 U4 8
9 Cable5/0/2 U5 8

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-28-2015 at 06:04 AM..
# 5  
Old 10-28-2015
Please use code tags as required by forum ruiles!

I have to second balajesuri in that additional info helps us help you. Howsoever, as first suggestions have already been posted, try this for your first request:
Code:
awk '/^!$/ {if (LAST) print NR - LAST; LAST = NR+1}' file
2
3
1
2

and for your second:
Code:
awk '
/^!$/           {if (2 == NR - LAST)    {for (i=1; i<=CNT; i++) print MEM[i]
                                         print ""
                                        }
                 LAST = NR+1
                 CNT = 0
                 delete MEM
                 next
                }
                {MEM[++CNT]=$0
                }
' file
5 Cable5/0/1 U0 4
5 Cable5/0/1 U1 4

9 Cable5/0/2 U4 8
9 Cable5/0/2 U5 8

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count lines

Hello, I have a file with two columns like the following FILE1: chr1 61042 chr1 61153 chr1 61446 chr1 61457 chr1 61621 chr10 61646 chr10 61914 chr10 62024 chr10 62782 Alos, I have another file FILE2: (13 Replies)
Discussion started by: rkk
13 Replies

2. Shell Programming and Scripting

Adding a lines to specific section of the file.

Hello, I have to a add 2 lines to /etc/sudoers file under this section below, can someone please suggest script to add these two lines when execute this remotely on to a multiple servers. before ## Allow root to run any commands anywhere root ALL=(ALL) ALL After ## Allow root... (2 Replies)
Discussion started by: bobby320
2 Replies

3. Shell Programming and Scripting

split continues lines to separated section with conditions

Hello; i have a file contains N continues records. i want to split these lines to some separate sections with each lines of a section has the desired condition compared to other sections input: AZR ? ? ? Pn 37.202 48.82 1136119044 1136119009 SHB ? ? ? Pn 37.802 48.02 1136119047 1136119008... (4 Replies)
Discussion started by: saeed.soltani
4 Replies

4. Shell Programming and Scripting

using awk to get specific section of lines in logs

i have a log file that has the date and time that looks like this: Wed Jun 28 15:46:21 2012 test failed tailed passed passed not error panic what we want to focus on is the first 5 columns because they contain the date and time. the date and time can be anywhere on the line. in this... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

How can i comment out a section between two particular lines

I want to find out which files under /etc have the the following section: and then i would like to comment out the above section in all the files. Please help. (3 Replies)
Discussion started by: proactiveaditya
3 Replies

6. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

Count certain lines

Hi! I have a file that looks like this: AAG ---------------------------------------------------------------------- Number of residues in the repeat = 3 AGA ---------------------------------------------------------------------- Number of residues in the repeat = 3 AGG ... (2 Replies)
Discussion started by: vanesa1230
2 Replies

8. Shell Programming and Scripting

Placing Duplicate Lines per section into another file

Hello, I need help in putting duplicate lines within a section into another file. Here is what I'm struggling with: Using this file “data.txt”: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5... (4 Replies)
Discussion started by: petersf
4 Replies

9. Shell Programming and Scripting

Removing Duplicate Lines per Section

Hello, I am in need of removing duplicate lines from within a file per section. File: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5 593.5000 ... (5 Replies)
Discussion started by: petersf
5 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question