Results 1 to 7 of 7

Thread: Pull only 6 character words from a text file

  1. #1
    Join Date
    Dec 2012
    Beans
    14

    Pull only 6 character words from a text file

    I cant seem to find much on the internet for this, hopefully you guys can help. I have a large list of names in a text file and I want to only extract the names with 6 characters and ignore anything greater or less than 6 characters in that word.

    would be nice to drop these results in a separate text file.
    Any suggestions?

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Pull only 6 character words from a text file

    How about 'grep' ?

    Code:
    $ cat names
    fred
    albert
    boris
    wendy
    george
    michael
    $
    $ grep -Ew '[[:alpha:]]{6}' names
    albert
    george

  3. #3
    Join Date
    Dec 2012
    Beans
    14

    Re: Pull only 6 character words from a text file

    That worked!

    1 more question, is there a way to capture that info and write into a separate text file?

    Forgive my noobness, only recently gave windows the finger and switch over to the ubuntu/linux world.

  4. #4
    Join Date
    Dec 2012
    Beans
    14

    Re: Pull only 6 character words from a text file

    nvm, >> filename ftw!

  5. #5
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Pull only 6 character words from a text file

    Code:
     grep -Ew '[[:alpha:]]{6}' names > textfile.txt
    Keep in mind that the “>” redirection operator always rewrittes files from the beginning. So if you don't want to rewrite a file, use the “>>” redirection operator, which will append the output to the end of the file without overwriting it.
    Last edited by slickymaster; January 28th, 2013 at 02:35 AM.

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Pull only 6 character words from a text file

    ^^^

    Also I forgot to mention if the names aren't one-per-line you may want to use the '-o' option e.g.

    Code:
    $ cat names2
    fred albert boris 
    wendy george 
    michael
    results in
    Code:
    $ grep -Ew '[[:alpha:]]{6}' names2
    fred albert boris 
    wendy george
    (probably not what you want) but

    Code:
    $ grep -Ewo '[[:alpha:]]{6}' names2
    albert
    george
    You can do

    Code:
    man grep
    to get a complete overview - have fun

  7. #7
    Join Date
    Dec 2012
    Beans
    14

    Re: Pull only 6 character words from a text file

    Thank you all for your input. It has all been useful. Thats what I love about the ubuntu community, instead of giving me a fish, you teach me how to fish!

Bookmarks

Posting Permissions

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