Help with grep at specific field of a file

hi,

I would like to search for a specific string at a specific line in a file and would like to know the count of it.

eg: samp.txt

ABC 1234 BELL
HNZ 4567 RING
NNN 5673 BELL

Please help with the command to find the count of BELL in the above file samp.txt at the specific line 10 with the length 4.

Thanks

cut -c10-13 samp.txt | grep BELL | wc -l

Using Perl:-

perl -wlane '
if (/\bBELL\b/ && $.==10 ) {
            map {$_ =~ /\bBELL\b/ and $i++ } @F ;
            print $i ;
            $i=0
}

'  sample.txt

;);):wink:

---------- Post updated at 18:30 ---------- Previous update was at 18:28 ----------

will not work if you have the word "BELLLLLLL" on the lines of the infile.

he specify that the word total length is 4.

awk ' substr($0,10,4) ~ /BELL/ { ++cnt } END { print cnt } ' file

@ahmad.diab
will not work if you have the word "BELLLLLLL" on the lines of the infile.

he specify that the word total length is 4.

Where did he mention that the 3rd field could exceed the 4 length ?

I based my code on the sample he gave , so if this sample does not represent all the cases he can get, then he has to give more clue.

The problem was not "i want to check that my 3rd field has a 4 character length"

see above BOLD line.

Maybe it's my english which is too bad ... i think i just don't understand the sentence in the same way than you:

when he wrote "Line 10", i thought it was a typo error and that we should read "char 10" instead (indeed, that was matching the sample he gave)

By the way, i read the bold line and he explicitly talk about 4 length, he never specified it could be larger .

Or am I missunderstanding something ?

I understood he wanted to count the number of BELL occurence appearing in field starting at char 10 and have a length of 4
(just as it appear in his example).
I didn't figure his problem was about having BELLLLLLLLLLLLL occurrence (if so, i suppose he would have put such an entry in the sample)

You are true that my code does not handle with BELLLLLLLLLLLLL : it count them as if it were a BELL one.
I just assumed that his input file was a file formatted the same way than the sample he gave.

may be he should explain to us what he want exactly. I hope he is reading his post now to feed us back.

BR