LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to check if a string inside file is longer than 50 characters. (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-check-if-a-string-inside-file-is-longer-than-50-characters-4175414296/)

secman110 07-01-2012 11:36 AM

How to check if a string inside file is longer than 50 characters.
 
Hi,

I am wondering if it is possible to use find and grep to search inside files to check if there is a string longer than 50 characters inside files.

Thanks,

rosehosting.com 07-01-2012 11:51 AM

To search in one file:
Code:

grep -e '[^\ ]\{50,\}' /path/to/file
Recursive search:
Code:

grep -Re '[^\ ]\{50,\}' /path/to/directory

David the H. 07-01-2012 12:20 PM

It would help if you gave a few more details. What constitutes a "string"? Do you mean whitespace-delimited characters? Whole lines? What? Perhaps you could give us a bit of sample text?

The suggestion given above searches for strings of 50 or more consecutive non-space characters. You can avoid the backslashes, however, by using the "-E" extended regex option, and you don't need to use "-e" when there's only a single expression.

Code:

grep -E '[^[:space:]]{50,}' /path/to/file
Notice that I also replaced the simple space with the "[:space:]" character class, which covers all whitespace characters.

"[^\ ]" is wrong in either version, BTW, as it means "not a backslash or a space". All simple characters are considered literal when inside a regex bracket expression except for "^","-", and "]", and even those are made unspecial by placing them in certain positions inside the brackets (i.e. "^" is only special if it's the first character in the list).

secman110 07-01-2012 01:22 PM

Here is the examples of the strings:

xGaaSZgwC2bw5Fl2RaWS1yw
2ecacebe01e77fe2dd40

Thank you!

secman110 07-01-2012 04:28 PM

Thank you all. I got my answer with your help.


All times are GMT -5. The time now is 01:23 AM.