grep a word from a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep a word from a line
# 1  
Old 12-23-2007
Computer grep a word from a line

Hi there,

Is there a way to grep a word from a line and display only the words beginning from the search word? Say I have a file named file.txt that contains below:

I am a file
Hello there Reader Orbix
I hope you feel good today

What I want is to grep the word "there":

$ grep "there" file.txt
Hello there Reader Orbix

and display only the words after the "there" together with it.

$ grep "there" file.txt | ....
there Reader Orbix

Or is there something else to do that?
# 2  
Old 12-23-2007
Code:
# awk 'match($0,"there"){print substr($0,RSTART)}' file
there Reader Orbix

# 3  
Old 12-23-2007
Hi.

A solution with sed:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate printing of specific token and following text.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sed

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

echo
echo " Results from sed:"
sed -n 's/\(.*\)\(there.*\)/\2/p' $FILE

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU sed version 4.1.2

 Input file data1:
I am a file
Hello there Reader Orbix
I hope you feel good today
There goes a bus.
not here.
Are you there?
Hear, hear!
Yes, Virginia, there is a Santa Claus

 Results from sed:
there Reader Orbix
there?
there is a Santa Claus

See man sed for details ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write a script for getout afew line and grep a define word

dear all Hi i want write a script can count number of my log file and every 5min run script for cgk log file for find a specific word such as Error for example in first 5 min we have 500 line in my log file and in second 5min we have 2500 line in my log file how can chk error word in my log... (4 Replies)
Discussion started by: Baber
4 Replies

2. Shell Programming and Scripting

How to grep nth word in line?

my input file content is like this GEFITINIB 403 14 -4.786873 -4.786873 -1.990111 0.000000 0.000000 -1.146266 -39.955912 483 VANDETANIB 404 21 -4.754243 -4.754243 -2.554131 -0.090303 0.000000 -0.244210 -41.615502 193 VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579... (4 Replies)
Discussion started by: chandu87
4 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

5. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

6. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

7. UNIX for Dummies Questions & Answers

How to grep word + next line in solaris 10?

Hi my unix friends, How do I grep a word and next line with solaris 10. tnx Mehrdad ---------- Post updated at 03:17 PM ---------- Previous update was at 03:09 PM ---------- I found this one: cat <file_name> | awk '/<seek_word>/ {print;getline;print}' (3 Replies)
Discussion started by: mehrdad68
3 Replies

8. Shell Programming and Scripting

grep a word instead of a line

Hi all, I'm a noobie at shell scripting (and Linux) with no experience on programing. I'm trying to create a script that will download my xml calendar from Google, process it in the format and dump it into $HOME/.calendar/calendar. Eg: April 10 Event I'm having a hard time using... (4 Replies)
Discussion started by: victorbrca
4 Replies

9. Shell Programming and Scripting

grep a word from a specific line

for example: searches only for single word for single word this is line three match=$(grep -n -e "single" data.txt) this command will stored "..... single ...... single" into search. how can i grep the single word just from line 2 only?? (3 Replies)
Discussion started by: blurboy
3 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question