Can grep command return word instead of complete line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Can grep command return word instead of complete line
# 1  
Old 12-28-2010
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:

HTML Code:
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 output:
HTML Code:
NitinrajSrivastava
VrajSrivastava
and not complete line.
# 2  
Old 12-28-2010
Try
Code:
sed 's/ \([a-zA-Z]*raj[a-zA-Z]*\) /\1,/g' file

R0H0N
# 3  
Old 12-28-2010
Hi
I tried the above command
however it is returning complete row rather than the word having the keyword.
# 4  
Old 12-28-2010
Which shell u r using. Show me your output.
R0H0N
# 5  
Old 12-28-2010
Hi R0H0N

I was trying this command
HTML Code:
echo 'Hi I am NitinrajSrivastava' | sed 's/ \([a-zA-Z]*raj[a-zA-Z]*\) /\1,/g'
It is returning complete line
# 6  
Old 12-28-2010
Code:
echo 'Hi I am NitinrajSrivastava' | sed -n 's/ \([a-zA-Z]*raj[a-zA-Z]*\) /\1,/g'

R0H0N
# 7  
Old 12-28-2010
Hi R0H0N

Thanks for the quick reply.
However the command
is not returning any row.

HTML Code:
echo 'Hi I am NitinrajSrivastava' | sed -n 's/ \([a-zA-Z]*raj[a-zA-Z]*\) /\1,/g'
</span>
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command is not search the complete pattern

I am facing a problem while using the grep command in shell script. Actually I have one file (PCF_STARHUB_20130625_1) which contain below records. SH_5.55916.00.00.100029_20130601_0001_NUC.csv.gz|438|3556691115 SH_5.55916.00.00.100029_20130601_0001_Summary.csv.gz|275|3919504621 ... (2 Replies)
Discussion started by: sumit.vedi1988
2 Replies

2. Shell Programming and Scripting

Need help in search for word and return line

Hi, I have this file format and can't seem to think of a solution. I need your help. I want to return lines which says "Record" if it's ID > 0 file.txt Record 1: :::::::::::::::::::::: :::::::::::::::::::::: ID "000001" :::::::::::::::::::::: :::::::::::::::::::::: ... (6 Replies)
Discussion started by: jakSun8
6 Replies

3. Solaris

Show complete command on command line when we use 'ps'

Hi, When I query using ps -ef, the complete command is not displayed and is truncated. Can you please tell me a method to resolve this ? I have tried the below. ps -ef | cat ps -ef | grep imp >/tmp/t1.txt /usr/ucb/ps -auxw pargs <pid> /usr/ucb/ps -aefyl | grep imp Thanks (11 Replies)
Discussion started by: mk1216
11 Replies

4. 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

5. Shell Programming and Scripting

grep return more than one line

Hi, I have a lot of log files which contain lines in the format of (date info) or (info), and when I use grep to search for "date" I was able to get the (date info) line, but some info lines are a separate line after the (date info) line... for example like: (date info info info) (date... (8 Replies)
Discussion started by: a27wang
8 Replies

6. UNIX for Advanced & Expert Users

Delete a word and complete line

Hi Canone please provide me solution how can achieve the result below: File1.txt $ sweet appleŁ1 scotish green $ This is a test1 $ sweet mangoŁ2 asia yellow $ This is a test 2 $ sweet apple red (there is no pound symbol here) germany green (1 Reply)
Discussion started by: Aejaz
1 Replies

7. UNIX for Dummies Questions & Answers

how to move word by word on command line

Hey All, On commad promt of a shell.. How can we move our cursor word by word. Like Ctrl+A takes to the starting of the command... Any shortcut like that..? Thanks pbsrinivas (1 Reply)
Discussion started by: pbsrinivas
1 Replies

8. UNIX for Dummies Questions & Answers

Using the Esc key to complete command line typing

Dear Techs, In the past on a different box (HP) I use to be able to complete something I was typing by entering a portion of the filename in the pwd and I would hit the Esc key and it would match the rest of the filename. I did this without understanding how it was setup. Now I am on a new... (1 Reply)
Discussion started by: jxh461
1 Replies

9. UNIX for Dummies Questions & Answers

grep for a line then return lines above

Hey guys, I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out? This is on Solaris 9..... I don't have GNU grep so -B and -A commands will not work (3 Replies)
Discussion started by: kingdbag
3 Replies

10. UNIX for Advanced & Expert Users

ls -R command but need complete path name on each line

Can anyone help me with the following: I need to traverse subdirectories to create a list of files with the pathname. For example, here's what I get with a simple ls -alR command: /MAIN/data/30007390 dte2>>ls -alR .: total 2 drwxrwx--- 4 ecfadmin staff 96 Oct 24 18:35 . ... (2 Replies)
Discussion started by: condor4-2
2 Replies
Login or Register to Ask a Question