Ubuntu Forums Archive Viewer

Pull only 6 character words from a text file

Archived thread 2109522 from Ubuntu Application Development. Markdown source: Ubuntu_Application_Development/thread_2109522_Pull_only_6_character_words_from_a_text_file.md

Original URL About this archive
#1

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

How about 'grep' ?

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

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

nvm, >> filename ftw!

#5
 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.

#6

^^^

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

$ cat names2
fred albert boris 
wendy george 
michael 

$ grep -Ew '[[:alpha:]]{6}' names2 fred albert boris wendy george

$ grep -Ew[COLOR=Red]o[/COLOR] '[[:alpha:]]{6}' names2 albert george

man grep

#7

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!