0

I have a file containing say the below strings for example.

File:
She sells sea shells on the seashore.

I would like to use grep (or any other grep like linux cmd tool) to be able to print whole words that match a particular substring/regular expression. In this case, I would like to print all word occurences in the file that match words starting with se in newlines. The required output is given below:

Required output:
sells
sea
seashore
lordlabakdas
  • 1,163
  • 5
  • 18
  • 33
  • grep and grep only?? – Jite Oct 07 '14 at 21:44
  • you are right....any tool would work actually...let me update the question :) – lordlabakdas Oct 07 '14 at 21:45
  • @Jite It's worth noting that the `regex` SO tag requests that users of that tag also include a tag for the language or tool for which they are having regex issues (since different langs/tools have different regex flavors and rules). So, it's good that `grep` was specified here. – ajp15243 Oct 07 '14 at 21:49
  • @ajp15243 Wow, I'll award you a gold medal in misunderstanding :) grep does regexp's really well, I just meant to ask if he specifically must use only grep as solution or if for instance a shell script later invoking grep would be just as good. – Jite Oct 07 '14 at 21:52
  • 1
    @Jite That's a rather rude way to tell me that I misunderstood your rather short and vague comment... – ajp15243 Oct 07 '14 at 21:56
  • @ajp15243 you could also think about this question as being a one stop for anyone looking for some linux commandline tool to carry out the task. of course, will update tags accordingly. – lordlabakdas Oct 07 '14 at 22:04
  • @jm666 oh no I did not, what makes you think I did? all i said was this question could turn into a more generic one as opposed to grep only. – lordlabakdas Oct 07 '14 at 22:31
  • @jm666 It wasn't lordlabakdas that said anything unsavory. I see his point and agree that this could be helpful as something rather generic to a Linux shell, and that I slightly misinterpreted the progression of comments and tag editing. Jite's comment is the one you are quoting. Nonetheless, thank you for stepping in when you felt something needed to be said. – ajp15243 Oct 08 '14 at 01:20
  • @lordlabakdas sorry, i really mean Jite. Deleted the comment. – clt60 Oct 08 '14 at 07:13
  • 1
    @ajp15243 Wasn't meant to insult you, sorry if you felt that way. I hoped the smiley would suggest it wasn't mean in a harsh way. – Jite Oct 08 '14 at 11:10
  • @Jite I probably wasn't in the greatest of moods when I read it too, so apology accepted and sorry for assuming you were being insulting. Score one for decency winning out on the Internet. – ajp15243 Oct 08 '14 at 14:29

3 Answers3

2

Use \< to anchor se to the beginning of the word, and \w* to add 0 or more word characters:

$ grep -o '\<se\w*' filename.txt 
sells
sea
seashore
$ 
Nick Russo
  • 1,522
  • 10
  • 13
  • Just a side note, but rather use single quotes instead of double to prevent shell expansion mistakes. – Jite Oct 08 '14 at 20:48
1
grep -oP '(^| )\Kse[^ .]*'

Output:

sells
sea
seashore
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

This will do the trick

grep -o '\bse\w*' filename.txt

Can anyone do it more pretty or see a reason this should not work?

brunsgaard
  • 5,066
  • 2
  • 16
  • 15