Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I create a shell script in unix/linux to find the number of lines in a text file that have words which are 5 to 10 character long and begin with a capital letter?

share|improve this question
    
I've heavily edited this question, please ensure it still conveys your original question. –  Steven D Apr 15 '11 at 5:03
    
Steven D.. that was a hard call :) it was difficult to know if dimpal meant "number of lines" or "line numbers"... I guess he'll pipe up one way or the other... –  Peter.O Apr 15 '11 at 14:29
    
@fred.bear: Indeed. Not sure which he meant so I allowed the given answer to guide me a bit. –  Steven D Apr 15 '11 at 21:56
    
I added an alternate solution in case he meant he wanted the line numbers that match. I think that should cover it. –  tak Apr 16 '11 at 0:54
add comment

2 Answers

egrep "^[A-Z]" FILE | egrep -c "\b[A-Za-z]{5,10}\b"

The way the question is now, it means, filter out lines, starting with a capital letter (like this line) [the line has to start with a capital letter - not the word], and include a word (not necessarily the first word) which is 5 to 10 characters long.

Yes, after reading the original question, there are 4 possibilities to interpret it: The Starting word has to fulfill both requirements or it is a starting word and another word, and: Get the line numbers, or the number of lines.

To print the line numbers for lines, which begin with a capital letter, and contain a word somewhere of length 5-10, we use sed:

sed -nr '/^[A-Z][a-z]{4,9}/=;/^[A-Z].*\b[A-Za-z]{5,10}\b/=' FILE 
share|improve this answer
    
It's hard to tell without some more info from the original poster, but I believe that is what @dimpal intended in the original question -- to count the number of lines that had at least one word between 5 and 10 characters long that starts with a capital letter. Also the solution you posted would match the line "F foobar", for example. –  tak Apr 17 '11 at 0:38
    
Yes, intentionally my first solution does. The line starts with a capital letter and contains a word of length 5-10. Think either: find the number of (lines in a text file that (have words which are 5 to 10 character long) and (begin with a capital letter)) OR find the number of lines in a text file that have (words (which are 5 to 10 character long) and (begin with a capital letter)). –  user unknown Apr 17 '11 at 0:55
    
The original question was "a shell script in unix/linux to find the lines number of a text file are having words which are 5 to 10 character long and having first letter as a capital letter", again, it's ambiguous but it sounds like he meant words that start with a capital letter, not lines that start with a capital letter. –  tak Apr 17 '11 at 0:59
    
Well, tak, I didn't vote you down or suggested your answer is wrong, but said, that I see 4 possibilities, and I provided what I think is a solution to 2 interpretations, which were not addressed by your solutions - no? Your interpretation might be better, but now I stop to argue, and work on an correction for my answers, which is needed. But I can vote your answer up, if it succeds my test cases. :) –  user unknown Apr 17 '11 at 1:52
    
Fair enough, no offense intended :) –  tak Apr 17 '11 at 3:01
show 1 more comment

Assuming the original question meant to count the number of lines matching the above requirements, then this should do it:

grep -e '\<[[:upper:]][[:alpha:]]\{4,9\}\>' file|wc -l

Assuming it meant to print the line numbers that match:

awk --re-interval '{line = line + 1} /\<[[:upper:]][[:alpha:]]{4,9}\>/ {print line}' file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.