Hi, When we ““grep”” for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Navin

Show file examples please. It matters how long fields are and what are separators.

IMHO, you should look into using an ’ awk’ command instead of using grep.
The command usage is well documented in the man pages and there are many
usage guides on google. It may be cryptic at first but after you use it a
few times awk will become a very useful command to have in your back
pocket.

Thanks

Hi

You can count the characters and use in cut command, if required output
comes in ms1 file,
then only cat ms1 file.

It’s working fine but find out no. of character before apply this command am I right?

Please let me know cat ms1.

I already know the what I need from file I can understand what you are telling but sometimes I don’t`t know happen with others but in my case. Suppose you want to display a particular user and redirect him to another file from /etc/password than how can we do this.

Hi

cut -c 1-10 /etc/squid/netuser > ms1

here 1-10 are field position of user

cat ms1 >> /etc/squid/download

No output with space.

Display all line I want to display particular world.

Sir I have two file /etc/squid/netuser and /etc/squid/download, netuser file contain htpasswd created user name and password information. I want to redirect only user not password line into /etc/squid/download file with grep and cat redirection how can it possible.

OK, I didn’t really think about the requirement. After all, if you just want words that match “foo” you’ll just get “foo” lots of times, which helps not a lot.

But what if the grep match is a pattern? Maybe, like a spelling checker, you want all words matching a pattern like “watch.*” so your output might contain watch, watchtower, watching, watchmaker, watched, watchable and so on.

Classical way to do this is to convert the input file to words, and then grep the list of words. Something like:

tr -c ‘[A-Za-z]’ ‘n’ | grep ‘^watch’

The tr -c (complement) option should convert all bytes except alphabetic to a newline. So every alphabetic word will now be on a line by itself, and no other characters appear. So the grep pattern will match and list any word that starts with “watch” (because the ^ anchors it to start of line).

Hi,

Grep command will search the given string in the file and produce the
entire matched line in the output
Here Mr. naveen doesn’t want entire lines in the output, how one can
understand the string at a particular position in a line,
grep -c option can give a count of matched strings in the file.

If you are talking about regular words you could try something like

grep " word " file

Notice the space between the " and the letter w and the space following the d

Hi,
You can read grep manual
–only-matching
–word-regexp
or without option
grep “work” file
or
find /home -name “sample” -exex grep “word” {} ;

Hi,
You can read grep manual
–only-matching
–word-regexp
or without option
grep “work” file
or
find /home -name “sample” -exex grep “word” {} ;

Hi,

Either use “-w” option
or insert space before and after of word

grep " book " FILE_NAME

Grep -o word file.