Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-01-2012, 11:36 AM   #1
LQ Newbie
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
How to check if a string inside file is longer than 50 characters.


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.

Old 07-01-2012, 11:51 AM   #2
Registered: Jun 2012
Location: Missouri, USA
Posts: 222

Rep: Reputation: 62
To search in one file:
grep -e '[^\ ]\{50,\}' /path/to/file
Recursive search:
grep -Re '[^\ ]\{50,\}' /path/to/directory
Old 07-01-2012, 12:20 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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.

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).
Old 07-01-2012, 01:22 PM   #4
LQ Newbie
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Here is the examples of the strings:


Thank you!
Old 07-01-2012, 04:28 PM   #5
LQ Newbie
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 3 03-21-2012 01:47 PM
[SOLVED] Shell Script Field longer than 20 characters karelvdm Programming 3 07-13-2011 06:24 AM
How to find a file with specific string inside it. pinga123 Linux - Newbie 5 05-07-2010 06:30 AM
How to find a string inside file in the whole disk penguinux Linux - Newbie 2 10-20-2008 07:34 AM
How to check for NULL characters in file ? sharathkv Programming 6 12-11-2006 11:14 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration