Find the answer to your Linux question:
Results 1 to 3 of 3
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 | ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9

    [SOLVED] 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).

    Edit: Okay, ended up using sed -n '/^.\{5\}$/p' file | more. Thanks for any help.
    Last edited by whocares357; 11-02-2010 at 08:19 PM.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    in case this is homework, the hint i will give you is to lookup negative lookahead assertions

    also probably look into using perl grep option

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9
    Can you at least explain why grep '.\{5\}' file | more returns four or more? I could maybe understand if it returned five or more, but why does it return four?

    Edit: I'm guessing that I have some hidden characters in there. How can I reveal them (vi or cat)? Nevermind: set list in vi.
    Last edited by whocares357; 11-02-2010 at 08:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •