LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   using grep (question) (https://www.linuxquestions.org/questions/linux-general-1/using-grep-question-704097/)

graziano1968 02-12-2009 02:38 AM

using grep (question)
 
Hello

using grep I wish to know if in a test.txt file if there are lines
containing more than 4 "." (more than 4 dots).

Any way to do this ?

Thank you

colucix 02-12-2009 02:49 AM

I'd prefer awk to parse a file line-by-line, but if you really want to use grep, you can try something like:
Code:

while read line
do
  echo $line | grep -o [.] | wc -l
done < file

This will count the number of dots in each line. You can add some control statement if you want to get only the lines for which the number of dots is greater than or equal to 4.

graziano1968 02-12-2009 02:56 AM

Quote:

Originally Posted by colucix (Post 3440681)
I'd prefer awk to parse a file line-by-line, but if you really want to use grep, you can try something like:
Code:

while read line
do
  echo $line | grep -o [.] | wc -l
done < file

This will count the number of dots in each line. You can add some control statement if you want to get only the lines for which the number of dots is greater than or equal to 4.

Thank you

grep -o [.] , does not seem to work , it only return a single "." for each line.

syg00 02-12-2009 03:42 AM

More than 4 consecutive dots regex can handle - more than 4 spread in an unknown pattern, that would require a deal of programming logic.
Sounds like a task for perl.

colucix 02-12-2009 03:47 AM

Quote:

Originally Posted by graziano1968 (Post 3440689)
grep -o [.] , does not seem to work , it only return a single "." for each line.

Actually it returns every single dot on a newline, in the sense that multiple dots are spread over multiple lines, but since you need just to count them (and not to display the matching line) it should do the trick. For example:
Code:

$ cat file
pippo.pluto.paperino.topolino
qui.quo.qua
nonna papera
$ while read line
> do
>  echo $line | grep -o [.] | wc -l
> done < file
3
2
0
$


syg00 02-12-2009 03:53 AM

I read the question differently - in my defence I blame poorly defined specifications.

... :eek:


All times are GMT -5. The time now is 07:29 AM.