count lines in a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count lines in a pattern
# 1  
Old 02-17-2011
count lines in a pattern

Hi,

I had posted few days back and got replies on how to extract patterns from a file.

I had another question. I want to count the number of lines a particular pattern. I thought of somethings like using NF variable, etc, but they didnt work.

Here is sample input.

Code:
---------------------
Date: 2007/07/10 08:08:02
Author: author1
Branch: HEAD
Tag: (none) 

Members: 
file 1
file2
file3

---------------------
Date: 2007/07/16 11:04:59
Author: author2
Branch: HEAD
Tag: (none) 

Members:
file1
file4
file5

---------------------
Date: 2007/07/30 08:57:45
Author: author3
Branch: HEAD
Tag: (none) 

Members:
file4
file5
file6
file7

I want to only find the number of lines, beneath the "Members:" field. i.e, the number of files beneath that for every record.

So, I just want sequence of numbers as output, for this example 3 3 4.

I tried some things trying to extract that pattern and trying to count the fields in them, but they didnt work. Any help on this will be very much appreciated.

Thanks,
Sandeep
# 2  
Old 02-17-2011
Try:
Code:
awk -vRS= -F\n '/Members/{print NF-1}' file

# 3  
Old 02-17-2011
Code:
awk '/file/{++x;a[y]=x}/Members/{x=0;y++}END{for (i=1;i<=y;i++) printf a[i] FS}'

# 4  
Old 02-17-2011
Bartus,

I tried your solution, but it is giving me the wrong numbers,
I can give you a sample file and the numbers I got with it,
Also, can you tell me what -vRS = -F\n does?

Input I gave

Code:
---------------------
PatchSet 3156 
Date: 2007/07/10 08:08:02
Author: mrennie
Branch: HEAD
Tag: (none) 
Log:
Bug 177554
[messages] remove internal error messages from translations

Members: 
    core/org/eclipse/debug/core/DebugPlugin.java:1.149->1.150 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupDirector.java:1.26->1.27 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupParticipant.java:1.8->1.9 
    core/org/eclipse/debug/core/sourcelookup/containers/CompositeSourceContainer.java:1.12->1.13 
    core/org/eclipse/debug/internal/core/BreakpointManager.java:1.93->1.94 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.java:1.27->1.28 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.properties:1.80->1.81 
    core/org/eclipse/debug/internal/core/ExpressionManager.java:1.54->1.55 
    core/org/eclipse/debug/internal/core/IConfigurationElementConstants.java:1.10->1.11 
    core/org/eclipse/debug/internal/core/LaunchConfiguration.java:1.101->1.102 
    core/org/eclipse/debug/internal/core/LaunchConfigurationComparator.java:1.6->1.7 
    core/org/eclipse/debug/internal/core/LaunchDelegate.java:1.10->1.11 
    core/org/eclipse/debug/internal/core/LaunchMode.java:1.10->1.11 
    core/org/eclipse/debug/internal/core/LogicalStructureProvider.java:1.7->1.8 
    core/org/eclipse/debug/internal/core/LogicalStructureType.java:1.14->1.15 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceContainerType.java:1.8->1.9 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceLookupMessages.java:1.10->1.11 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceLookupMessages.properties:1.17->1.18 
    core/org/eclipse/debug/internal/core/sourcelookup/SourcePathComputer.java:1.5->1.6 

---------------------
PatchSet 3162 
Date: 2007/07/16 11:04:59
Author: mrennie
Branch: HEAD
Tag: (none) 
Log:
Bug 177554
[messages] remove internal error messages from translations

Members: 
    core/org/eclipse/debug/core/DebugPlugin.java:1.150->1.151 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupDirector.java:1.27->1.28 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupParticipant.java:1.9->1.10 
    core/org/eclipse/debug/core/sourcelookup/containers/CompositeSourceContainer.java:1.13->1.14 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.java:1.28->1.29 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.properties:1.81->1.82 
    core/org/eclipse/debug/internal/core/LaunchConfiguration.java:1.102->1.103 
    core/org/eclipse/debug/internal/core/LaunchMode.java:1.11->1.12 
    core/org/eclipse/debug/internal/core/LogicalStructureProvider.java:1.8->1.9 
    core/org/eclipse/debug/internal/core/LogicalStructureType.java:1.15->1.16 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceContainerType.java:1.9->1.10 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceLookupMessages.java:1.11->1.12 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceLookupMessages.properties:1.18->1.19 

---------------------
PatchSet 3185 
Date: 2007/07/30 08:57:45
Author: mrennie
Branch: HEAD
Tag: (none) 
Log:
Bug 191379 Console encoding does not match workspace encoding

Members: 
    buildnotes_platform-debug.html:1.1898->1.1899 
    core/org/eclipse/debug/core/DebugPlugin.java:1.151->1.152 
    core/org/eclipse/debug/core/ILaunchManager.java:1.69->1.70 
    core/org/eclipse/debug/internal/core/LaunchConfiguration.java:1.104->1.105 
    core/org/eclipse/debug/internal/core/LaunchManager.java:1.201->1.202 

---------------------
PatchSet 3186 
Date: 2007/07/30 09:34:53
Author: darin
Branch: HEAD
Tag: (none) 
Log:
Bug 191379 - Console encoding does not match workspace encoding

Members: 
    META-INF/MANIFEST.MF:1.13->1.14 
    core/org/eclipse/debug/core/DebugPlugin.java:1.152->1.153 

---------------------
PatchSet 3189 
Date: 2007/07/31 05:40:49
Author: cwindatt
Branch: HEAD
Tag: (none) 
Log:
Bug 177554 - [messages] remove internal error messages from translations

Members: 
    core/org/eclipse/debug/core/DebugPlugin.java:1.153->1.154 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupDirector.java:1.28->1.29 
    core/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupParticipant.java:1.10->1.11 
    core/org/eclipse/debug/core/sourcelookup/containers/AbstractSourceContainer.java:1.10->1.11 
    core/org/eclipse/debug/core/sourcelookup/containers/AbstractSourceContainerTypeDelegate.java:1.4->1.5 
    core/org/eclipse/debug/core/sourcelookup/containers/CompositeSourceContainer.java:1.14->1.15 
    core/org/eclipse/debug/core/sourcelookup/containers/LocalFileStorage.java:1.7->1.8 
    core/org/eclipse/debug/core/sourcelookup/containers/ZipEntryStorage.java:1.6->1.7 
    core/org/eclipse/debug/internal/core/BreakpointManager.java:1.94->1.95 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.java:1.29->1.30 
    core/org/eclipse/debug/internal/core/DebugCoreMessages.properties:1.82->1.83 
    core/org/eclipse/debug/internal/core/LaunchConfiguration.java:1.105->1.106 
    core/org/eclipse/debug/internal/core/LaunchDelegate.java:1.11->1.12 
    core/org/eclipse/debug/internal/core/LaunchMode.java:1.12->1.13 
    core/org/eclipse/debug/internal/core/LogicalStructureProvider.java:1.9->1.10 
    core/org/eclipse/debug/internal/core/LogicalStructureType.java:1.16->1.17 
    core/org/eclipse/debug/internal/core/sourcelookup/SourceContainerType.java:1.10->1.11 

---------------------
PatchSet 3192 
Date: 2007/08/02 07:31:06
Author: darin
Branch: HEAD
Tag: v20070802 
Log:
Bug 177556  [messages] remove internal error messages from translations
    - added @since 3.4 for new ERROR constant

Members: 
    core/org/eclipse/debug/core/DebugPlugin.java:1.154->1.155


Output I got

Code:
74
43
18
3
63
2

@Yinyuemi, I did not get any output when I tried the code you gave.
# 5  
Old 02-17-2011
Code:
awk '/core\//{++x;a[y]=x}/Members/{x=0;y++}END{for (i=1;i<=y;i++) printf a[i] RS}'

# 6  
Old 02-17-2011
Try now:
Code:
awk -vRS= -vFS="\n" '/Members/{print NF-1}' file

# 7  
Old 02-17-2011
using Solaris' "nawk":
Code:
nawk '/Members/{print NF-1}' RS= FS= myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

4. UNIX for Advanced & Expert Users

Count number of lines between a pattern in a large file

1000CUS E Y4NYRETAIL 10010004HELIOPOLIS 110000500022360591000056XX EG 1101DEBY XXAD ZSSKY TSSROS 1102HANYNNYY@HOTMAIL.COM 210030/05/201301/06/2013AED 3100 OPE 3100 CLO 3100 The 1000CUS E Y NYCORPORATE 10010004HELIOPOLIS 110000500025270504550203XX EG 1101XXXQ FOR CTING AND... (1 Reply)
Discussion started by: john2022
1 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

9. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 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