How to count words number for each paragraph?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count words number for each paragraph?
# 1  
Old 07-15-2013
How to count words number for each paragraph?

Hi, I have file like this:

Code:
>number1
dlejreoreltedgerere
dgtrtryr
>number2
dkfdjlgdjfeiflefjdfidlfjdifelfjefe
fjdlfjdkfjdlfdjlfdjlfjdigeo
gjelreoureofouererg
>number4
dklfdjfoeueoruer
fjeorueotueoirueorueorueore
gjoeoueorueoreuroerueor
joeuroerueorue

How can i know how many words for each ">number" ?

Thanks!!

Last edited by Scrutinizer; 07-15-2013 at 01:04 PM.. Reason: code tags
# 2  
Old 07-15-2013
You mean you wish to count the number of lines after a separator pattern is matched?
Code:
perl -ne 'if (/>(number\d+)/){print "$num $count\n";$num=$1;$count=0;}else{$count++}' filename

# 3  
Old 07-15-2013
I want to know how many words. For example:

Code:
>number1
dlejreoreltedgerere
dgtrtryr

number1 (from "d“ to ”r") has 27 words in total



Quote:
Originally Posted by Skrynesaver
You mean you wish to count the number of lines after a separator pattern is matched?
Code:
perl -ne 'if (/>(number\d+)/){print "$num $count\n";$num=$1;$count=0;}else{$count++}' filename


Last edited by Scrutinizer; 07-15-2013 at 01:04 PM.. Reason: code tags
# 4  
Old 07-15-2013
I presume, you mean characters, not words, excluding newline characters, spaces ?
Try something like:
Code:
awk 'NR>1{printf "%s: ", $1; $1=x; print gsub(/./,x)}' OFS= RS=\> file

or
Code:
awk '/>/{if(t) print t; printf "%s: ",$2; t=0; next}{t+=length} END{print t}' FS=\> file

Output:
Code:
number1: 27
number2: 80
number4: 80

# 5  
Old 07-15-2013
Thanks!!
The code works great!!

Quote:
Originally Posted by Scrutinizer
I presume, you mean characters, not words, excluding newline characters, spaces ?
Try something like:
Code:
awk 'NR>1{printf "%s: ", $1; $1=x; print gsub(/./,x)}' OFS= RS=\> file

or
Code:
awk '/>/{if(t) print t; printf "%s: ",$2; t=0; next}{t+=length} END{print t}' FS=\> file

Output:
Code:
number1: 27
number2: 80
number4: 80

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count unique words

Dear all, I would like to know how to list and count unique words in thousands number of text files. Please help me out thanks in advance (9 Replies)
Discussion started by: imranrasheedamu
9 Replies

2. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

3. Shell Programming and Scripting

problem to count number of words from file

hi every one i have written this simple shell for counting number of word that user need to find from file but i have get several error when run it. can someone tell me the problem ? echo "Enter the file name" read file echo "enter word" read word for i in \`cat $file` do if then... (1 Reply)
Discussion started by: nimafire
1 Replies

4. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

5. Shell Programming and Scripting

Count number of match words

Input: some random text SELECT TABLE1 some more random text some random text SELECT TABLE2 some more random text some random text SELECT TABLE3 some more random text some random text SELECT TABLE1 some more random text Output: 'SELECT TABLE1' 2 'SELECT TABLE2' 1 'SELECT TABLE3' 1 I... (5 Replies)
Discussion started by: chitech
5 Replies

6. Shell Programming and Scripting

Count the number of words in some subset of file and disregard others

Hi All, I have some 6000 text files in a directory. My files are named like 1.txt, 2.txt 3.txt and so on until 6000.txt. I want to count the "number of words" in only first 3000 of them. Any suggestions? I know wc -w can count the number of words in a text file. I am using Red Hat Linux. (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Shell Programming and Scripting

Count words

Hi, does anyone know the command to count words by its length. I need to Count the number of five letter words that I have in a file with thousand of words. thanks (3 Replies)
Discussion started by: fabioamaury
3 Replies

9. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

10. Shell Programming and Scripting

Count the no of lines between two words

Please help in the following problem: Input is: Pritam 123 456 Patil myname youname Pritam myproject thisproject iclic Patil remaining text some more text I need the command which will display the no of lines between two words in the whole file. e.g. Display all the no of lines... (5 Replies)
Discussion started by: zsudarshan
5 Replies
Login or Register to Ask a Question