LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-02-2010, 03:56 PM   #1
whocares357
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Rep: Reputation: Disabled
Pattern Searching


I have a file with a random word on each line (3k+ lines).

How can I get the lines with only five characters? I tried using grep '.....' file | more, but it returns all the words (even those less than 5 characters).

Edit: I also tried grep '.{5}' file | more but it doesn't show anything. And grep '.\{5\}' file | more returns all lines with four or more characters (I'm really confused about why it's doing this).
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-02-2010, 04:08 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
It's not the most elegant solution but I'd do:

Code:
sed -n '/^.....$/p' file
 
2 members found this post helpful.
Old 11-02-2010, 04:13 PM   #3
whocares357
LQ Newbie
 
Registered: Oct 2010
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
It's not the most elegant solution but I'd do:

Code:
sed -n '/^.....$/p' file
Hm, I must have some hidden characters in the text file because my output consists of all four character lines. Is there any way to reveal hidden characters (in vi or cat)? Nevermind: set list in vi.

Edit: Thank you very much. I actually simplified the expression with '/^.\{5\}$/p'.

Last edited by whocares357; 11-02-2010 at 04:18 PM.
 
1 members found this post helpful.
Old 11-02-2010, 04:41 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Excellent. That's much better.
 
Old 11-02-2010, 06:06 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by whocares357 View Post
Hm, I must have some hidden characters in the text file because my output consists of all four character lines. Is there any way to reveal hidden characters (in vi or cat)? Nevermind: set list in vi.

Edit: Thank you very much. I actually simplified the expression with '/^.\{5\}$/p'.
Hi,

to further "simplify" you could use sed's -r option:
Code:
sed -r -n '/^.{5}/p' infile
This way you do not have to escape the curly braces {}. Ok, it is not really simplifying but readability improves - especially if you have to use a lot of {()} ...
 
  


Reply


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] Searching a directory and pulling out filenames with a certain pattern LadyAnne Linux - Newbie 5 05-17-2010 10:30 AM
searching pattern in tab delimited file using grep vaibhavs17 Programming 5 03-05-2010 02:02 AM
Searching for a PATTERN in source files RECURSIVELY dsuratman Linux - General 6 11-23-2007 02:51 AM
pattern searching - kindly help hinetvenkat Linux - Software 2 08-09-2005 12:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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