LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2011, 07:40 AM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
next word of a particular word in linux


Hi all,

In linux is there a way to find the next word of a particular word of a file.

grep displays entire line of the particular word.

But i want only the exact next word of that particular word.is there any command for that.


Thanks in advance,
Dinesh.
 
Old 02-09-2011, 08:00 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not exactly sure what you mean. It is often best to give an example. As far as grep goes, have a look at the -o option will give you only the text you are looking for and
maybe the -m option which allows you to stop after a particular number of matches.
 
Old 02-09-2011, 08:02 AM   #3
vonedaddy
Member
 
Registered: Aug 2004
Location: Philadelphia,PA
Posts: 185

Rep: Reputation: 17
I am not sure I understand your question. If you know the next word of a particular word is going to be that word, whats the point?

Do you want to know how many times a particular word appears in a text file?
Do you want to know on which line is the next appearance of a particular word?

In my mind there would be know reason for a command that just returns a word that you already know.

For example.. Let stay I have a text file containing all the text above this line.

grep word file.txt
The above command would return all the lines containing the word, therefore giving you some context as to how the word was being used, or where it was in the file.

A word search as your asking can be done in editors like vi and nano. In vi you can use the following command (while in command mode) to find the next "word".


/word
 
Old 02-09-2011, 08:04 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think that what the OP wants is for a file that contains this line:

Code:
stuff stuff theword next more stuff
searching for the word "theword" will return "next".
 
Old 02-09-2011, 08:09 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ cat file
In linux is there a way to find the next word of a particular word of a file.
grep displays entire line of the particular word.
But i want to find only the exact next word of that particular word.is there any command for that.

$ ruby -ne 'puts $_.scan(/find\s+(\w+)/)' file
the
only
 
Old 02-09-2011, 08:10 AM   #6
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
Hi all,

for example i am having

Name Indians are great

in a file. if i use "grep Name <filename>" then the output would be "Name Indians are great"


But i want only "Indians" to be displayed if i use "grep Name <filename>"

i.e. the next word to "Name".

Last edited by dinakumar12; 02-09-2011 at 08:13 AM.
 
Old 02-09-2011, 08:17 AM   #7
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
I think you can use awk for this, For example:

Code:
echo Name Indians are great | awk '{for(i = 1; i<=NF; i++) if($i=="Name") print $(i+1);}'
prints Indians

So you Can pipe the output of grep to it.

Last edited by Ashkan_s; 02-09-2011 at 08:27 AM. Reason: Correcting
 
1 members found this post helpful.
Old 02-09-2011, 08:26 AM   #8
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
Hi Ashkan,

Thanks it works,

But i want some thing like awk '{for(i =0; i<NF; i++) if($i=="Name") print $(i+1);}' <filename>

then it should print indians.

This text "Name Indians are great" would be inside a file.
 
Old 02-09-2011, 08:35 AM   #9
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
Could it be a little different?

grep the text in the file the pipe its output to awk, like this:
Code:
grep "Name" <filename> | awk '{for(i = 1; i<=NF; i++) if($i=="Name") print $(i+1);}'
it should work.

P.S.
I edited the first code a little it was wrong, (for(i =0; i<NF; i++) must be for(i = 1; i<=NF; i++))

Last edited by Ashkan_s; 02-09-2011 at 09:23 AM.
 
1 members found this post helpful.
Old 02-09-2011, 09:02 AM   #10
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
Hi Ashkan_s,

Thanks that works.
 
Old 02-09-2011, 09:23 AM   #11
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
You're Welcome

And it must be
Quote:
grep "Name" <filename>
not
Quote:
grep "Name Indians are great" <filename>
I'll edit it.
 
Old 02-09-2011, 09:25 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Why so complicated:
Code:
awk '/Name/{getline;print;exit}' RS=" " <filename>
If there are more possible versions of Name in the file that you want as well, simply remove the exit
 
2 members found this post helpful.
Old 02-09-2011, 09:36 AM   #13
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
Talking

Because I am not much familiar with awk.

Thanks for that.
 
Old 02-09-2011, 09:50 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A sed alternative:
Code:
sed -nr "s/.*Name (\w+) *.*/\1/p" file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 10:12 AM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
Problems Copying & Pasting In Word When Word Closes - Ubuntu davidx Linux - Software 3 10-22-2008 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration