I have a file that has a story contained in it, and I need to practice using either grep or sed. I have tried egrep -i '^w.' file.txt and it gets most of the words, but I need every word starting with w ("Paperwork" is invalid, "working" is valid).


Probably you'd need a regexp to isolate word start (\b) and recognize word characters, e.g. \S, see documentation for more. So you could use the command

$ grep -oE '\bw\S*' inputfile

With -o, the result will be only the matching parts, with each match on a new line (even from the one input line), and -E to be able to use "extended regex" (the same as using egrep).

A more strict "word" match might use [[:alpha:]] instead of \S:

$ grep -oE '\bw[[:alpha:]]*' inputfile

That would result in "words" (character sequences) in the English character set, starting with "w".

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.