1829645 Members
9689 Online
109992 Solutions
New Discussion

Find words from a file

 
SOLVED
Go to solution
WW451512
Advisor

Find words from a file

Hi All,

How can I find out all the words starting from 'd' and ending with 'g' from a file.

Thanks..
6 REPLIES 6
Elmar P. Kolkman
Honored Contributor

Re: Find words from a file

Commando would be something like this:

$ sed -e 's|[ ][ ]*|\n|g' -e 's|[.,:;||g' | grep '^d.*g$'

(Between the '[' and ']' are a space and a TAB !)

Explanation: the sed will put all words on a single line, cutting out common reading-symbols. The grep will get all lines starting with a 'd' and ending with a 'g'.
Every problem has at least one solution. Only some solutions are harder to find.
James R. Ferguson
Acclaimed Contributor

Re: Find words from a file

Hi:

# grep -E -w "d.+g" file

The '-w' switch enables "word" mode for 'grep'. The regular expression matches a word that begins with "d", is followed by one or more characters, and ends with "g". See the manpages for more information.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Find words from a file

>JRF: The '-w' switch enables "word" mode for 'grep'. The regular expression matches a word that begins with "d", is followed by one or more characters, and ends with "g".

This doesn't do what you expected. It matches a word that starts with "d" and any number of chars, then ends in a possibly different word with "g":
d sig

This may work better:
grep -E -w "d[A-Za-z_0-9]+g"
Raj D.
Honored Contributor
Solution

Re: Find words from a file

# awk '{for(i=1;i<=NF;++i) if($i~/^d.*g$/) print $i }' file
" If u think u can , If u think u cannot , - You are always Right . "
Dennis Handly
Acclaimed Contributor

Re: Find words from a file

>find out all the words starting from 'd' and ending with 'g' from a file.

Do you want the words separated or just the lines with the words?
JRF's and my solution does the latter.
WW451512
Advisor

Re: Find words from a file

Thanks to all for replies..

I was looking only for the words (not lines) starting and ending with specific letters.

This was served by using Raj's solution.